【问题标题】:Swift: Performing segue unexpectedly斯威夫特:意外地执行segue
【发布时间】:2016-03-31 07:09:01
【问题描述】:

我有这个用于验证表单的操作按钮,但我按下此按钮,会提示警报,当警报被解除(按“确定”)时,它会执行对主 VC 的 segue。按下按钮后,我想留在同一个 VC 中。:

@IBAction func loginButton(sender: AnyObject) {

        if self.password.text == "" || self.email.text == "" {

            self.displayAlert("Error", message: "Please insert email and password")
            print("if")

        }


        }


    func displayAlert(title: String, message: String) {

        // cria a mensagem de alerta
        var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)

        // adiciona botao a mensagem de alerta
        alert.addAction((UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in

            //o que faz quando o botao da mensagem de alerta é apertado.
            self.dismissViewControllerAnimated(true, completion: nil)


        })))

        //apresenta a mensagem.
        self.presentViewController(alert, animated: true, completion: nil)



    }

任何想法我做错了什么? 附言。我检查了网点,没有任何技巧。

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    删除这一行:

    self.dismissViewControllerAnimated(true, completion: nil)
    

    这是在用户按下 OK 后发生的,并且警报视图已经关闭。 您在那里所做的是关闭显示警报的视图。

    【讨论】:

      【解决方案2】:

      你应该这样做。

      alert.addAction((UIAlertAction(title: "Ok", style: .Cancel, handler: nil))
      

      如果您想在按下 ok 后执行某些操作,请在处理程序中执行此操作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-22
        • 2017-03-24
        • 1970-01-01
        • 1970-01-01
        • 2016-02-08
        • 2017-11-13
        • 2015-08-19
        • 2014-09-11
        相关资源
        最近更新 更多