Swift - 推送警报动作以显示新的ViewController

时间:2016-10-19 22:32:03

标签: ios swift uialertcontroller

如何在警报按钮上显示或链接到新的ViewController?

enter image description here

这是我的代码

let alert = UIAlertController(title: validateQRObj.responseDescription, message: validateQRObj.productName, preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default) { (action) -> Void in
    let viewController = self.storyboard?.instantiateViewController(withIdentifier: "ProductDetialViewController")
    self.present(viewController!, animated: true, completion: nil)
}
alert.addAction(action)
self.present(alert, animated: true, completion: nil)

2 个答案:

答案 0 :(得分:3)

控制从View Controller 1(黄点)拖动到View Controller 2上的任何位置,然后单击Segue。显示属性检查器和Under Storyboard Segue标识符名称标识符VC2

如果这是你要找的答案,除了答案之外别忘了。

func alert(){

    let alertController = UIAlertController(title: "Open View Controller. ", message: "Press  Ok  to open View Controller number 2.", preferredStyle: UIAlertControllerStyle.alert)
    let ok = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: {(action) -> Void in
    //The (withIdentifier: "VC2") is the Storyboard Segue identifier.
    self.performSegue(withIdentifier: "VC2", sender: self)
 })

    alertController.addAction(ok)
    self.present(alertController, animated: true, completion: nil)
}

答案 1 :(得分:0)

对我来说它不起作用。我做了:

    func alert(){
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let twop = storyboard.instantiateViewController(withIdentifier: "Accueil") as! Accueil
    let alertController = UIAlertController(title: "Open new View !", message: "Clic to ok", preferredStyle: UIAlertControllerStyle.alert)
    let ok = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: {(action) -> Void in
        self.navigationController?.show(twop, sender: self)
    })

    alertController.addAction(ok)
    self.present(alertController, animated: true, completion: nil)
}

现在它正在工作!!