Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift3 Swift 3.0中UIAlertAction内部的UIAlertView?_Swift3_Appdelegate_Uialertcontroller_Uialertaction - Fatal编程技术网

Swift3 Swift 3.0中UIAlertAction内部的UIAlertView?

Swift3 Swift 3.0中UIAlertAction内部的UIAlertView?,swift3,appdelegate,uialertcontroller,uialertaction,Swift3,Appdelegate,Uialertcontroller,Uialertaction,是否可以在UIAlertAction中添加UIAlert视图 因为当我试图在UIAlertAction中添加UIAlert视图时,它说 “警告:试图显示其视图不在窗口中的对象。” 等级制度!” 这是我的密码 let myAlert = UIAlertController(title: "Title", message: "title here", preferredStyle: UIAlertControllerStyle.alert) let okaction = UIAlertAction(

是否可以在UIAlertAction中添加UIAlert视图

因为当我试图在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)

尝试将调用包装到函数中的第二个警报,然后调用该函数

像这样

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

尝试在
navigationController
上显示
AlertController

更改行

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

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

假设我上面的代码在SecondViewController中,如果(statement==1){}正在使用上面的代码使其工作。但是,如果我在它下面有另一个“if(statement==2){}”,并且我想显示警报而不去rootViewController该怎么办呢。我的意思是它可以留在SecondViewController中吗?@AlotJai然后你需要写这两行
appDelegate.window?.rootViewController=navigationController appDelegate.window?.makeKeyAndVisible()
inside
if(statement==1){}