如何在iOS 9的alert.addAction中按下OK时打开另一个视图控制器

问题描述 投票:1回答:3

我想显示一个名为InViewController的viewcontroller,当按下add.alertAction中的“OK”时。

if ((user) != nil) {               
   let alert = UIAlertController(title: "Success", message:   "Logged In", preferredStyle: .Alert)
   alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
   self.presentViewController(alert, animated: true){}
}
ios swift
3个回答
2
投票
let alert = UIAlertController(title: "Success", message: "Logged In", preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default) { (action) -> Void in
  let viewControllerYouWantToPresent = self.storyboard?.instantiateViewControllerWithIdentifier("SomeViewControllerIdentifier")
  self.presentViewController(viewControllerYouWantToPresent!, animated: true, completion: nil)
}
alert.addAction(action)
self.presentViewController(alert, animated: true, completion: nil)

3
投票

您可以在添加完成时将其添加到UIAlertAction中,以便按照以下方式添加它:

if ((user) != nil) {               
    let alert = UIAlertController(title: "Success", message:   "Logged In", preferredStyle: .Alert)

    let OKAction = UIAlertAction(title: "OK", style: .Default, handler: { _ -> Void in 
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("ViewControllerA") as! ViewControllerA
        self.presentViewController(nextViewController, animated: true, completion: nil)
    })

    alert.addAction(OKAction)
    self.presentViewController(alert, animated: true){}
}

要设置StoryboardID,可以在Identity Inspector中使用Interface Builder,请参见下图:

enter image description here

我在上面的代码中引用ViewControllerA的所有内容,你必须根据你的需要设置你的UIViewController的名称。

编辑:

您指向StoryBoard上的UIView或其他对象。按下其他物体顶部的黄色指示灯,即UIViewController,如下图所示:

enter image description here

我希望这对你有帮助。


0
投票

这就是你如何做到这一点,我只是更新Victor Sigler的优秀作品

你通过这个小小的更新来回答他的答案。

 private func alertUser( alertTitle title: String, alertMessage  message: String )
{
   let alert = UIAlertController(title: title, message: message,   preferredStyle: .alert)
    let actionTaken = UIAlertAction(title: "Success", style: .default) { (hand) in
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let destinationVC = storyBoard.instantiateViewController(withIdentifier: "IntroPage") as? StarterViewController
        self.present(destinationVC!, animated: true, completion: nil)

    }
    alert.addAction(actionTaken)
    self.present(alert, animated: true) {}
}
© www.soinside.com 2019 - 2024. All rights reserved.