2015-02-04 3 views
0

저는 UIAlertController에 대한 많은 자습서를 보았습니다. 지금까지 내가 발견 한 방법은 단추 또는 레이블에 UIAlertController를 연결 한 다음 IBAction을 호출하여 UIAlertController를 활성화하는 것입니다.사용자 입력없이 UIAlertController를 만들고 닫으려면 어떻게해야합니까? (Swift)

사용자가 앱을 시작할 때 경고를 자동으로 표시하도록 코드를 복제하려고했습니다. 사용자에게 자습서를 계속 사용할지 묻고 싶었습니다. 그러나, 나는 오류가 계속 :

Warning: Attempt to present UIAlertController on MainViewController whose view is not in the window hierarchy!

가 그럼 난 addChildViewController 및 addSubview를 통해 MainViewController에 UIAlertController을 추가했습니다. 그러나 나는 오류가 발생합니다 :

Application tried to present modally an active controller

나는 presentViewController 기능을 사용할 수 없다고 생각하고 주석 처리했습니다.

UIAlertController가 표시되지만 취소 또는 절대로 버튼을 클릭하려고하면이 오류가 발생합니다.

Trying to dismiss UIAlertController with unknown presenter.

정말 엉망입니다. 누군가 내가 잘못하고있는 것을 나눌 수 있습니까? 정말 고맙습니다. 여기에 코드가 있습니다.

func displayTutorial() { 

    alertController = UIAlertController(title: NSLocalizedString("tutorialAlert", comment: ""), message: NSLocalizedString("tutorialMsg", comment: ""), preferredStyle: .ActionSheet) 

    self.addChildViewController(alertController) 
    self.view.addSubview(alertController.view) 
    alertController.didMoveToParentViewController(self) 
    alertController.view.frame.origin.x = self.view.frame.midX 
    alertController.view.frame.origin.y = self.view.frame.midY 
    //alertController.popoverPresentationController?.sourceView = self.view*/ 


    let OkAction = UIAlertAction(title: NSLocalizedString("yesh", comment: ""), style: .Destructive) { (action) in 

    } 
    alertController.addAction(OkAction) 

    let cancelAction = UIAlertAction(title: NSLocalizedString("notNow", comment: ""), style: .Destructive) { (action) in 
     //println(action) 
     self.tutorial = 1 
     self.presentedViewController?.dismissViewControllerAnimated(true, completion: nil) 
    } 

    alertController.addAction(cancelAction) 

    let neverAction = UIAlertAction(title: NSLocalizedString("never", comment: ""), style: .Cancel) { (action) in 
     self.tutorial = 1 
    } 
    alertController.addAction(neverAction) 


    //self.presentViewController(alertController, animated: false) {} 

} 

답변

0

해결책을 찾았습니다. 분명히, 나는 func viewDidLoad에서 UIAlertController를 호출 할 수 없다. viewDidAppear에서 함수를 호출해야합니다. Warning: Attempt to present * on * whose view is not in the window hierarchy - swift

: 그래서 내 코드는 지금이 게시물에

override func viewDidAppear(animated: Bool) { 
    if tutorial == 0 { 
     displayTutorial(self.view) 
    } 
} 

func displayTutorial(sender:AnyObject) { 

    let alertController = UIAlertController(title: NSLocalizedString("tutorialAlert", comment: ""), message: NSLocalizedString("tutorialMsg", comment: ""), preferredStyle: .ActionSheet) 


    let OkAction = UIAlertAction(title: NSLocalizedString("yesh", comment: ""), style: .Destructive) { (action) in 

    } 
    alertController.addAction(OkAction) 

    let cancelAction = UIAlertAction(title: NSLocalizedString("notNow", comment: ""), style: .Default) { (action) in 
     //println(action) 
     self.tutorial = 1 
     self.presentedViewController?.dismissViewControllerAnimated(true, completion: nil) 
    } 

    alertController.addAction(cancelAction) 

    let neverAction = UIAlertAction(title: NSLocalizedString("never", comment: ""), style: .Cancel) { (action) in 
     self.tutorial = 1 
    } 
    alertController.addAction(neverAction) 

    self.presentViewController(alertController, animated: true, completion: nil) 

    if let pop = alertController.popoverPresentationController { 
     let v = sender as UIView 
     pop.sourceView = view 
     pop.sourceRect = v.bounds 
    } 
} 

감사입니다

관련 문제