2017-04-05 1 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

보십시오.

명기

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

감사. 그것은 작동합니다. – AlotJai

+0

위의 코드가 SecondViewController 안에 있고 if (statement == 1) {}가 위의 코드를 사용하여 작동하게하는 경우를 가정 해 보겠습니다. 하지만 그 아래에 if (statement == 2) {} "다른 것이 있으면 rootViewController에 가지 않고 경고를 표시하고 싶습니다. 내 말은 그것이 SecondViewController에 있어야한다는 것입니다. 가능합니까? – AlotJai

+0

@AlotJai 그런 다음'appDelegate.window? .rootViewController = navigationController appDelegate.window? .makeKeyAndVisible()'if (statement == 1) {}'안에이 두 줄을 써야합니다. –

0

함수에서 두 번째 경고에 대한 호출을 래핑 한 다음 해당 함수를 호출하십시오.

이렇게하면됩니다. navigationControllerAlertController을 제시

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

관련 문제