2017-02-09 23 views
0

點擊我需要做兩個動作。顯示該消息,然後轉到另一個用戶界面。但這裏只是任務1執行而不是任務2爲什麼點擊它沒有做任務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) 
} 
+0

查看控制檯。應該有一個錯誤消息告訴你這個問題。 – rmaddy

+0

並且您的任務2代碼正在執行。使用調試器來確認。 – rmaddy

回答

2

打電話給你的任務,在動作裏面,原因UIAlertController是一個視圖控制器,它已經提出,如果要呈現附加了同樣的看法,你需要先解僱當前的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) 
相關問題