I am using an unwind segue to unwind to the initial view controller in my storyboard. The unwind works great, I implemented this method in my initial view controller:
- (IBAction) unwindToInitialViewController:(UIStoryboardSegue *) unwindSegue {
}
However if I try an segue to another view controller after I do the unwind I get the following error:
Warning: Attempt to present on whose view is not in the window hierarchy!
It seems like this only occurs if I unwind to the view controller that is checked as 'Initial View Controller' in the storyboard. Is this a bug? Should I be able to unwind to that initial controller? Other ideas?
EDIT:
Here is how I perform the second segue:
[self performSegueWithIdentifier:@"mySegue" sender:nil];
I should note that this is a login/logout problem. When I log in the first time the segue from my login controller to my next controller works. When I logout I unwind to the initial view controller. I then log in again and the segue from my login controller to the next controller does not work.
EDIT 2:
From more research I have found its because I am using a delegate for my login. Login is async, I make a call with AFNetworking and when its done I call my login delegate (the login VC in this case). At that point the login VC can segue to the view.
Login code:
- (void) login: (NSDictionary *) parameters {
[http.manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, NSDictionary *response) {
[self.loginDelegate loginSuccess:response];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self.loginDelegate loginFailure:error];
}];
}
My login VC which is the delegate:
- (void) loginSuccess:(NSDictionary *) response {
// setup user info based on response
...
// Segue
[self performSegueWithIdentifier:@"loginSuccessSegue" sender:nil];
}
I have checked that I am in the main thread when and I segue and still no luck. I know that AFNetworking always calls the success/failure blocks on the main thread too.
The tricky part. If I change that above code to use a block and not a delegate the storyboard/segue does not get messed up and I can login and logout many times with no problem.
Why does the segue work the first time with the delegate pattern, but on logout (unwind), can I not use that segue again?
EDIT 3:
More investigation shows that on unwind my login VC viewDidAppear is called twice. On initial unwind the view looks to still be on the stack, show it shows quickly and viewDidAppear is called. However this is animated away quickly and viewWillAppear is called a second time with a different VC. I think this might be the root of the problem. Why when I unwind to that VC is it animated away only to be animated back in?