【问题标题】:Swift 2 - How do I switch to another ViewController when the AlertController appears?Swift 2 - 当 AlertController 出现时如何切换到另一个 ViewController?
【发布时间】:2015-12-28 21:37:25
【问题描述】:

单击 AlertController 上的“确定”后,我正尝试切换到另一个 ViewController。目前,当我单击“确定”时,它会停留在当前的 ViewController 上。如何才能做到这一点?谢谢。

此代码显示在 ViewController1 中,当 AlertController 出现时单击“确定”后,我想更改为 ViewController4。

代码如下:

  @IBAction func submitTapped(sender: AnyObject) {
    print("Validating...")
    validator.validate(self)
    Button1.hidden = false
}

// MARK: ValidationDelegate Methods

func validationSuccessful() {
    print("Validation Success!")
    let alert = UIAlertController(title: "Success", message: "You are validated!", preferredStyle: UIAlertControllerStyle.Alert)
    let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: {action in ViewController4))
        alert.addAction(defaultAction) - Error Code // Variable used within its own initial value
        self.presentViewController(alert, animated: true, completion: nil)

        } - Error Code //Expected ‘,’ separator

【问题讨论】:

    标签: swift segue viewcontroller


    【解决方案1】:

    您需要向OK 操作添加处理程序:

    let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: {action in
        performSegueWithIdentifier("controller4SequeIdentifier", sender: nil);
    })
    alert.addAction(defaultAction)
    self.presentViewController(alert, animated: true, completion: nil)
    

    在处理程序中,您可以切换到另一个控制器。请注意,这将在用户按下OK 按钮后发生。您还需要在情节提要中向 ViewController4 添加一个序列,并为其提供一个标识符以匹配传递给 performSegueWithIdentifier 的标识符。

    【讨论】:

    • 我已经添加了那行代码,但我仍然遇到问题。 let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: {action in ViewController4() alert.addAction(defaultAction) - 错误代码 // 变量在它自己的初始值中使用 self.presentViewController(alert, animated: true, completion: nil) } - 错误代码 //预期的 ',' 分隔符
    • 我认为你没有正确放置结束}
    • 我在这篇文章中对上述代码进行了更改,以显示我在哪里得到了错误。
    • @GurvierSinghDhillon 我已经更新了答案中的代码示例,现在应该很清楚了。
    • 为此干杯,错误已经消失。我已经运行了该应用程序,当我单击“确定”时,它不会更改为另一个视图控制器。你说的我都听了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多