如何呈现位于另一个UIViewController中的UIAlertController

时间:2017-08-23 19:00:29

标签: ios swift uialertcontroller

我收到以下错误:

  

尝试在Check5GHz上出现:其视图不是   在窗口层次结构中!

SplashViewController:

let check5ghz = Check5GHz()
check5ghz.determineIf5GHz()

以下是Check5GHz的样子:

class Check5GHz: UIViewController {

func determineIf5GHz()-> Void{

 let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
}

所以似乎问题是一个ViewController试图启动在另一个ViewController中找到的UIAlertController,它似乎不允许。但是我需要从许多不同的ViewControllers中看到这个UIAlertController。你能推荐另一种方式吗?

1 个答案:

答案 0 :(得分:1)

extension UIViewController {
    func determineIf5GHz()-> Void{
        let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Click", style: 
        UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}

然后在任何视图控制器中执行此操作:

self.determmineIf4GHz()