CodeRatchet CodeRatchet -5 years ago 126
iOS Question

Segue performing on seperate button click

I'm following a course online, the app I'm building is basically a cut down version of Instagram as you can see on the left hand side I have a view for signing in, on the right hand side I have a view for signing up.

Pretty straight forward right, now I have created a

from the signin page from the button
which when pressed takes you to the registration view (one on the right), this works as expected however if I'm on the signin page and click sign in and it errors for whatever reason I display the error message but straight after is performs the
to the registration screen even though I never pressed

This is my storyboard:

enter image description here

This is my code behind the
Sign In
button located on the left view

@IBAction func btnSignIn(sender: AnyObject) {

if txtUsername.text == "" || txtPassword.text == "" {

displayAlert("Error", message: "Username and Password required!")


I have no other code inside this controller which would cause the
to initialise.

If someone can shed some light into how I can stop this
from happening on
Sign In
click and only happen when I press
I'd appreciate it.


After further investigation this seems to be an issue with my alert box I'm displaying. If I comment out the alert box and press signin and let it error yet not display anything then click register which takes me to the view on the right, click sign in and get taken back to view on the left and then press
Sign In
display the alert box and when it closes it seems like it thinks the view controller that's being displayed in the one previous to the current one on the page, which is odd. This is my alert function:

func displayAlert(title: String, message: String) { // Display alert message to user. Passing in title and message which will be displayed.

if #available(iOS 8.0, *) {
let a = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)

a.addAction((UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in

self.dismissViewControllerAnimated(true, completion: nil)


self.presentViewController(a, animated: true, completion: nil)

} else {

// Fallback on earlier versions


Answer Source

You don't need to dismiss the alert view controller in the action. When the user selects an action, the alert controller is automatically dismissed.

Since you are calling dismissViewControllerAnimated you are going back to the previous view controller.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download