swift - 当呈现给具有 webview 的视图控制器时不显示警报
问题描述
我试图在我的视图控制器中显示警报框,它有一个使用 WKWebView 的 web 视图。我有一个侧边菜单,我在另一个继承 UIView 的类中创建,并且该侧边菜单有一个注销选项。所以我想要它只要我点击在注销选项上,侧面菜单应从超级视图中删除,并发出警报。
我已经创建了sidemenu并在单击选项时删除了视图。现在我只想显示警报。但这没有发生。
func logout(){
let alert = UIAlertController(title: "", message: "Do you want to logout from the application", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))
let vc = WebViewController()
vc.present(alert, animated: true, completion: nil)
}
单击注销选项后,应立即出现警报。
解决方案
当你写的时候问题就在这里
let vc = WebViewController()
创建 WebViewController() 的新对象,以便警报出现在您创建的新对象中,而不是在前一个对象中创建或从其他视图调用函数 logout()
所以你需要传递 UIViewController 的对象,你需要在其中显示警报
func logout(vc: UIViewController){
let alert = UIAlertController(title: "", message: "Do you want to logout from the application", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))
vc.present(alert, animated: true, completion: nil)
}
怎么打电话
logout(vc: self)
推荐阅读
- javascript - 在 DataTables 中使用 onchange 函数
- mysql - 从两个不同的页面向数据库提交数据
- asp.net-core - 将 Asp.net Core Identity 添加到受 Identity Server 保护的 API
- css - css,当您垂直居中内联块元素时
- python - 索引错误:形状不匹配:索引数组无法与形状一起广播
- python - 如何在 python pandas 配对图中包含时间范围?
- python - 如何使用来自另一个 csv 文件的数据写入现有 CSV 文件?
- django - Django速率限制未运行
- objective-c - Objective C 中的 Lottie 3.0+ 动画
- javascript - 数据未被正确接受