SwiftでViewControllerにあるボタンをクリックした時にalertがでます.
そこで"本当に戻りますか?"とalertを出した時に
"戻る"を選択したら
元のViewControllerのnavigationControllerで戻るを実装したいのですがなかなかうまくいきません.
どのようにしたらAlertViewControllerで画面遷移を実装できますか?
ちなみに以下のように試してみたりしたのですけれども...
alertのactionのクロージャに書いてみたりとか,
swift
1 // UIAlertControllerを作成する. 2 let myAlert: UIAlertController = UIAlertController(title: "戻る", message: "本当に戻りますか?", preferredStyle: .Alert) 3 4 // OKのアクションを作成する. 5 let backAction = UIAlertAction(title: "戻る", style: .Default) { action in 6 print("Action OK!!") 7 self.navigationController?.popToViewController(navigationController!.viewControllers[0], animated: true) 8 } 9 let cancelAction = UIAlertAction(title: "キャンセル", style: .Cancel, handler: nil) 10 11 myAlert.addAction(logoutAction) 12 myAlert.addAction(cancelAction) 13 // UIAlertを発動する. 14 presentViewController(myAlert, animated: true, completion: nil)
フラグを立ててみたり(こちらはうまくいかないと思ったけれどもやってみてうまくいかなかったw)
swift
1 var segueFlag = false 2 // UIAlertControllerを作成する. 3 let myAlert: UIAlertController = UIAlertController(title: "戻る", message: "本当に戻りますか?", preferredStyle: .Alert) 4 5 // OKのアクションを作成する. 6 let backAction = UIAlertAction(title: "戻る", style: .Default) { action in 7 print("Action OK!!") 8 segueFlag = true 9 } 10 let cancelAction = UIAlertAction(title: "キャンセル", style: .Cancel, handler: nil) 11 12 myAlert.addAction(logoutAction) 13 myAlert.addAction(cancelAction) 14 // UIAlertを発動する. 15 presentViewController(myAlert, animated: true, completion: nil) 16 if segueFlag{ 17 self.navigationController?.popToViewController(navigationController!.viewControllers[0], animated: true) 18 } 19
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。