如何在没有用户输入的情况下创建和关闭UIAlertController? (迅速)

时间:2015-02-04 03:52:30

标签: swift uiviewcontroller uialertcontroller

我一直在寻找关于UIAlertController的很多教程。到目前为止,我发现的方法是通过将UIAlertController链接到按钮或标签然后调用IBAction来激活UIAlertController。

我尝试复制代码,以便在用户进入应用程序时自动弹出警报(我想询问用户是否要查看教程)。但是,我一直收到错误:

  

警告:尝试在MainViewController上显示UIAlertController,其视图不在窗口层次结构中!

然后我尝试通过addChildViewController和addSubview将UIAlertController添加到MainViewController。但是,我收到错误:

  

应用程序尝试以模态方式呈现活动控制器

我认为我无法使用presentViewController函数并将其注释掉。

显示UIAlertController但是当我尝试点击取消或从不按钮时,会发生此错误。

  

尝试使用未知的演示者解雇UIAlertController。

我真的很难过。有人可以分享我做错了吗?非常感谢。这是代码。

    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) {}

}

2 个答案:

答案 0 :(得分:0)

我找到了解决方案。显然,我无法从func viewDidLoad调用UIAlertController。我必须从viewDidAppear调用该函数。所以我的代码现在是

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
    }
}

感谢此帖:Warning: Attempt to present * on * whose view is not in the window hierarchy - swift

答案 1 :(得分:0)

带有扩展名的UIAlertController下面的内容将帮助您显示带有动态索引的按钮数量的警报,并带有针对所选索引的完成处理程序

extension UIViewController {

func displayAlertWith(message:String) {
    displayAlertWith(message: message, buttons: ["Dismiss"]) { (index) in
    }
}

func displayAlertWith(message:String, buttons:[String], completion:((_ index:Int) -> Void)!) -> Void {
    displayAlertWithTitleFromVC(vc: self, title: Bundle.main.infoDictionary!["CFBundleDisplayName"] as! String, andMessage: message, buttons: buttons, completion: completion)
}

func displayAlertWithTitleFromVC(vc:UIViewController, title:String, andMessage message:String, buttons:[String], completion:((_ index:Int) -> Void)!) -> Void {

    let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
    for index in 0..<buttons.count    {

        let action = UIAlertAction(title: buttons[index], style: .default, handler: {
            (alert: UIAlertAction!) in
            if(completion != nil){
                completion(index)
            }
        })
        alertController.addAction(action)
    }

    DispatchQueue.main.async {
        vc.present(alertController, animated: true, completion: nil)
    }
}

}

如果您需要自动关闭警报,可以在延迟一段时间后在显示的视图控制器上调用关闭

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
        vc.dismiss(animated: true, completion: nil)
    }

希望这对您有所帮助。