为什么完成不起作用?完成问题

时间:2019-01-29 12:49:39

标签: swift completionhandler

为什么补全不起作用?

警报出现后,它不会过渡到另一个viewController

self.present(self.alertController2, animated: true, completion:  {                          
    self.performSegue(withIdentifier:"goToLogin", sender: self)
})

2 个答案:

答案 0 :(得分:1)

Self.present用于将此操作添加到视图中。您需要将此代码添加到handler处理程序的意思是It means what will I do when you click OK

let alertbutton = UIAlertAction(title: "OK", style: .destructive, handler: { 
(UIAlertAction) in
            self.performSegue(withIdentifier: "your id", sender: self)
        })

完整示例:

let alert = UIAlertController(title: "Its empty", message: "Going another view", preferredStyle: .alert)
        let alertaction = UIAlertAction(title: "OK", style: .destructive, handler: { (UIAlertAction) in
            self.performSegue(withIdentifier: "your segue id", sender: self)
        })
        alert.addAction(alertaction)
        self.present(alert, animated: true, completion: nil)

答案 1 :(得分:0)

您应该先编写代码,然后再进入新的视图代码运行,然后在主线程中显示警报以显示警报。

DispatchQueue.main.async {
//                    AlertView.showAlert(title: Messages.Network.title, message: Messages.Network.message)
//                }