2017-04-05 8 views
2

UIAlertAction内にUIAlertビューを追加することはできますか?Swift 3.0のUIAlertAction内のUIAlertView?

私はUIAlertAction内UIAlertビューを追加しようとしたとき、それは

と言うので、「警告:そのビューに提示する試みは、ウィンドウ 階層ではありません!」

ここに私のコードです。

let myAlert = UIAlertController(title: "Title", message: "title here", preferredStyle: UIAlertControllerStyle.alert) 
let okaction = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: 
    { 
     action in 
     let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController 

     let appDelegate = UIApplication.shared.delegate as! AppDelegate 
     let navigationController = UINavigationController.init(rootViewController: myViewController) 
     appDelegate.window?.rootViewController = navigationController 
     appDelegate.window?.makeKeyAndVisible() 

     if (statement here == 1) { 
      let myAlert = UIAlertController(title: "Title", message: "title", preferredStyle: UIAlertControllerStyle.alert) 
      myAlert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil)) 
      self.present(myAlert, animated: true, completion: nil) 
      return 
     } 
    } 
) 
myAlert.addAction(okaction) 
self.present(myAlert, animated: true, completion: nil) 

答えて

1

navigationControllerAlertControllerを提示するようにしてください。

著名

navigationController.present(myAlert, animated: true, completion: nil) 
+0

ありがとうございます。それは働く。 – AlotJai

+0

私のコードがSecondViewControllerの内側にあり、if(statement == 1){}が上記のコードを使って動作しているとします。しかし、if(文== 2){}が他のものよりも下にあり、rootViewControllerに行かずに警告を表示したい場合はどうすればいいですか?私はそれはSecondViewControllerにとどまっているということですそれは可能ですか? – AlotJai

+0

@AlotJai次に、これらの2行の 'appDelegate.window?.rootViewController = navigationController appDelegate.window?.makeKeyAndVisible()'を 'if(statement == 1){}'の中に書く必要があります。 –

0

関数内の2番目の警告への呼び出しをラップし、その関数を呼び出してみてください。

このようにします。

let myAlert = UIAlertController(title: "Title", message: "title here", preferredStyle: UIAlertControllerStyle.alert) 
let okaction = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: { action in 

    let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController 

    let appDelegate = UIApplication.shared.delegate as! AppDelegate 
    let navigationController = UINavigationController.init(rootViewController: myViewController) 
    appDelegate.window?.rootViewController = navigationController 
    appDelegate.window?.makeKeyAndVisible() 

    if (statement here == 1) { 
     self.callAlert() 
    } 
}) 

myAlert.addAction(okaction) 
self.present(myAlert, animated: true, completion: nil) 

func callAlert() { 
    let myAlert = UIAlertController(title: "Title", message: "title", preferredStyle: UIAlertControllerStyle.alert) 
    myAlert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil)) 
    self.present(myAlert, animated: true, completion: nil) 
} 
+0

で変更ライン

self.present(myAlert, animated: true, completion: nil) 

。ありがとう:) – AlotJai

関連する問題