2017-02-09 2 views
0

クリックすると2つの操作を行う必要があります。メッセージを表示し、別のユーザーインターフェイスに移動します。しかし、ここだけTASK 1を使用すると、さらに同じ見解を提示したい場合はTASK 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は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) 
関連する問題