Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

objective c - Segue crashes my program. Has something to do with my NavigationController and my TabBarViewController

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

My StoryBoard setup

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

enter image description here


enter image description here


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

enter image description here


enter image description here

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".

enter image description here


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()
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...