2016-11-01 58 views
2

我以前遇到过类似的问题,UIAlertController,因为在UIAlertController被解雇后,UI线程总会有滞后。
我现在的问题是,如果用户点击“Okay”UIAlertAction,我想执行一个segue,如果按下“Cancel”UIAlertAction,则不会发生任何事情。
这是我的代码:UIAlertController解除后执行segue

// create the uialertcontroller to display 
let alert = UIAlertController(title: "Do you need help?", 
           message: "Would you like to look for a consultant?", 
           preferredStyle: .alert) 
// add buttons 
let okay = UIAlertAction(title: "Yes, please.", style: .default, handler: {_ in 
    self.performSegue(withIdentifier: "segue", sender: nil) 
}) 
let no = UIAlertAction(title: "No, I'm okay.", style: .cancel, handler: nil) 
alert.addAction(okay) 
alert.addAction(no) 
self.present(alert, animated: true, completion: nil) 

什么正在发生的事情,当我点击“好”的SEGUE正在执行,但我只能看到过渡的最后时刻(即动画开始,而UIAlertController是为被解雇)。
一旦UIAlertController被解雇,我该如何让这个segue开始?

注 - 我不喜欢用像hacky方法解决这个问题,就像在一个固定的延迟后执行segue,如果还有其他方法的话。

谢谢!

+0

@马特所以不是一个真正的办法做到这一点,除非我'n'秒后动画?此外 - 没有我说的禁止 – uti0mnia

回答

3

的问题是在此代码:

let okay = UIAlertAction(title: "Yes, please.", style: .default, handler: {_ in 
    self.performSegue(withIdentifier: "segue", sender: nil) 
}) 

handler:不是完成处理程序。它在之前运行警报被(自动)解除。因此,当警报仍然存在时,您正在启动继续。

如果你不喜欢使用delay(虽然我认为没有错的方法),我会尝试是这样的:

let okay = UIAlertAction(title: "Yes, please.", style: .default, handler: {_ in 
    CATransaction.setCompletionBlock({ 
     self.performSegue(withIdentifier: "segue", sender: nil) 
    }) 
}) 
+0

我想 - 有没有什么办法可以在控制器被解雇后执行此操作? – uti0mnia

+0

我怎么告诉你不要?如果你指的是我的笔记,那就意味着我当然更喜欢一种没有静态调度的方法:) – uti0mnia

+0

你的答案不包含派遣后,所以我不明白为什么想学习一个好的解决方案是不公平的? – uti0mnia