简体   繁体   中英

Swift: Performing segue unexpectedly

I've this action button used to validate a form, but I press this button, the alert is prompted and when the alert is dismissed (pressing "Ok"), it performs a segue to the main VC. I'd like to stay in the same VC after the button is pressed.:

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



    }

Any ideas what I am doing wrong? Ps. I've checked the outlets and there is no trick with them.

Remove this line:

self.dismissViewControllerAnimated(true, completion: nil)

This is happens after the user pressed OK, and the alert view is already dismissed. What you did there, is to dismiss the view that showed the alert.

you should do something like this.

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

and if you want to perform something after ok pressed , do that in handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM