Segue crashes my program. Has something to do with

2019-01-20 16:22发布

问题:

I encountered a problem while testing my app. You can see how I have setup my views in the following image:

The thing is that everything works fine. I user my test user to login, and the following code executes the loginSegue,

self.performSegueWithIdentifier("loginSegue", sender: self)

which is a modal segue that links my login "View Controller" with the "Home Tab Bar View Controller". I get redirected to the "Initial Feed View Controller". Everything works great. But when I go to my "Settings View Controller", and click on the Logout button, which has the following code (IBAction):

@IBAction func logoutAction(sender: AnyObject) {

  PFUser.logOut()

  self.performSegueWithIdentifier("logoutSegue", sender: self)
}

my app crashes. My Logout button's action has a segue that takes you back to the login "View Controller", but logs the user out first (I am using Parse by the way). This "Push" segue is called logoutSegue. I tried to change the segue to be a "Popover" segue. That solves the issue, kinda, since by doing so it messes up my "Registration View Controller" since when in the login "View Controller" I click on the button "Register" and the app crashes anyways.

The compiler tells me that the following line of code is the one causing the error:

self.performSegueWithIdentifier("logoutSegue", sender: self)

I do not understand why this is happening. I suppose that it has something to do with how I have my "Navigation Controller" and "Home Tab Bar View Controller" setup. Or maybe with Delegates?

Could you help me to fix this? It does not matter if you have an Objective-C approach, please feel free to suggest a solution and I can try to convert it from Objective-C to Swift.

Besides helping me fixing my issue, I was wondering if you know what is the difference between the type of Segues (Push, Modal, Popover, Replace) and when should I use each. I read Apple's documentation but I still don't understand it in its' entirety.

Thank you in advance for your advice and responses.

回答1:

You do not want to use a traditional segue to return to your login screen. The normal segue types always create a new copy of the destination view controller. Instead, you want to return control to the view controller that called you.

You need to set up an unwind segue on logout. Here's how.

1) In the code for ViewController add this function:

@IBAction func comeHereOnLogout(segue:UIStoryboardSegue) {
    println("Yay, Logged Out!")
}

2) Control-drag from the view controller icon at the top of your Settings View Controller to the exit icon at the top of your Settings View Controller and choose comeHereOnLogout from the pop up that appears.



Select that segue in the Document Outline and give it an Identifier in the Attributes Inspector such as "logoutSegue".


Then you can trigger this segue in code with:

self.performSegueWithIdentifier("logoutSegue", sender: self)

3) Alternatively, you can wire the unwind segue from your Logout button to the exit icon at the top of your Settings View Controller. In this case, this segue would replace your logout button action. Again, you'd want to give this segue a name such as "logoutSegue".


In this case, you'd put your logout code in prepare for segue:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if segue.identifier == "logoutSegue" {
        PFUser.logOut()
    }
}