点击它之后为什么不执行任务2?

时间:2017-02-09 17:03:29

标签: ios swift

点击后我需要做两个动作。显示消息然后转到另一个用户界面。但是这里只有TASK 1正在执行但不是TASK 2.

@IBAction func sendMessage(_ sender: AnyObject) {

    // TASK 1 - OK    
    let alert = UIAlertController(title: "Task 1", message: "Test message.", preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))    
    self.present(alert, animated: true)

    // TASK 2 - FAIL (does not execute)
    let x = XyzViewController()
    x.body = "test";
    self.present(x, animated: false, completion: nil)
}

1 个答案:

答案 0 :(得分:2)

在操作内部调用你的任务,因为UIAlertController是一个viewcontroller,它已经显示,如果你想另外显示相同的视图,你需要首先关闭当前的VC,但是在这里这不是一个好的实践。

// TASK 1 - OK    
let alert = UIAlertController(title: "Task 1", 
                              message: "Test message.", 
                              preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: .default) { action in
  // perhaps use action.title here
  let x = XyzViewController()
  x.body = "test";
  self.present(x, animated: false, completion: nil)
})

self.present(alert, animated: true)