if文の評価結果によりUIAlertControllerを表示するコードを書いたのですが、時に二回,さんかい連続で表示する場合などはif文を評価してくれません。なので、本来はそうはしたくないのですが、一つのアラートが表示されている上にもう一つのアラートが表示されてしまいます。
いろいろ調べたら
連続で表示するときは、その都度親ビューコントローラを取得すればいいとのことだったのですが、
swiftでの表記が少なく見様見真似でのコードになってしまいました。どのように変更したらいいでしょうか。
Swift
1 2 func judge(){ 3 4 // ① UIAlertControllerクラスのインスタンスを生成 5 let alert: UIAlertController = UIAlertController(title:OK, message:OK, preferredStyle:.alert) 6 // ② Actionの設定 7 // OKボタン 8 let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style:.default,handler:{ 9 // ボタンが押された時の処理を書く(クロージャ実装) 10 (action:UIAlertAction!) -> Void in 11 }) 12 13 // ③ UIAlertControllerにActionを追加 14 alert.addAction(cancelAction) 15 16 // ④ Alertを表示 17 var baseview:UIViewController = UIApplication.shared.keyWindow!.rootViewController! 18 while(baseview.presentedViewController != nil && !baseview.presentedViewController!.isBeingDismissed){ 19 baseview=baseview.presentedViewController! 20 } 21 baseview.present(alert5, animated: true, completion: nil) 22 23 } 24 25@IBAction func OKTouch(_ sender: UIButton) { 26 //aが3でないならアラート0表示 27 if(a != 3){ 28 29 // ① UIAlertControllerクラスのインスタンスを生成 30 let alert: UIAlertController = UIAlertController(title:OK, message:OK, preferredStyle:.alert) 31 // ② Actionの設定 32 // OKボタン 33 let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style:.default,handler:{ 34 // ボタンが押された時の処理を書く(クロージャ実装) 35 (action:UIAlertAction!) -> Void in 36 }) 37 38 // ③ UIAlertControllerにActionを追加 39 alert.addAction(cancelAction) 40 41 // ④ Alertを表示 42 var baseview:UIViewController = UIApplication.shared.keyWindow!.rootViewController! 43 while(baseview.presentedViewController != nil && !baseview.presentedViewController!.isBeingDismissed){ 44 baseview=baseview.presentedViewController! 45 } 46 baseview.present(alert5, animated: true, completion: nil) 47 48 //aが3でない時、かつが3でない時アラート1表示 49 if(b != 3){ 50 51 // ① UIAlertControllerクラスのインスタンスを生成 52 let alert1: UIAlertController = UIAlertController(title:OK, message:OK, preferredStyle:.alert) 53 // ② Actionの設定 54 // OKボタン 55 let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style:.default,handler:{ 56 // ボタンが押された時の処理を書く(クロージャ実装) 57 (action:UIAlertAction!) -> Void in 58 }) 59 60 // ③ UIAlertControllerにActionを追加 61 alert1.addAction(cancelAction) 62 63 // ④ Alertを表示 64 var baseview:UIViewController = UIApplication.shared.keyWindow!.rootViewController! 65 while(baseview.presentedViewController != nil && !baseview.presentedViewController!.isBeingDismissed){ 66 baseview=baseview.presentedViewController! 67 } 68 baseview.present(alert1, animated: true, completion: nil) 69 } 70 } 71 72 judge() 73} 74
エラー
Warning: Attempt to present <one.thirdViewController: 0x7fb1f05d9b70> on <one.secondViewController: 0x7fb1f0888800> which is already presenting <UIAlertController: 0x7fb1f073ed70>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/11/13 04:48