Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unwind segue / performSegueWithIdentifier: problems

in my app, there is a chatting service. when the user wants to message someone new, he press on "new chat" and a Table View Controller is shown to select from the list of friends. i'm using unwind segues in order to go back to the previous view controller and share the data between the two view controllers (mainly the data will be the friend to have a chat with). the unwind segue works perfectly, however in my app, when the user goes back to the main VC, i fire a new segue in order to go to another VC directly where the user has the chat; and this isn't working. All segues are well connected and i tried NsLog in every corner and it's entering there and even the prepareForSegue: is being accessed. i tried putting an NsTimer, thought there might be some technical conflict, and it didn't work. i change the segue to the chat VC to a modal and now it's giving me this error:

Warning: Attempt to present on whose view is not in the window hierarchy!

i can access this VC in other ways and it's in the hierarchy. my questions are: what could be wrong? does unwind segues alter the windows hierarchies ?

PICTURE: enter image description here

to present the problem more visually, the main VC i'm talking about is on the bottom left connected to a navigation view controller. when a user presses new chat, the two VC on the top right are presented (one to choose between friends or group, the other to show the friends/ groups). so when a friend is selected for say from the top right VC i should unwind segue to the main VC. as you can see from the main VC there is other segues. non of them can work if i do unwind segue and they do work if i operate normally.

like image 302
HusseinB Avatar asked Dec 05 '22 08:12

HusseinB


1 Answers

The reason it is not working and is giving you that error is because things arent happening in the order you think they are.

When the unwind happens the view which is visible is not dismissed yet, therefore you are trying to perform a segue on a view which is in fact not in that hierarchy like the error says, take this for example, placing NSLog statements in the final view and then in the unwind method in your main view controller you can see the following:

2013-11-27 14:51:10.848 testUnwindProj[2216:70b] Unwind
2013-11-27 14:51:10.849 testUnwindProj[2216:70b] Will Appear
2013-11-27 14:51:11.361 testUnwindProj[2216:70b] View Did Disappear

Thus the unwind in the main view is getting called, the view will appear (your main view controller), and then your visible view is dismissed. This could be a simple fix:

@interface ViewController () {
    BOOL _unwindExecuted;
}

@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"Will Appear");
    if (_unwindExecuted) {
        _unwindExecuted = NO;
        [self performSegueWithIdentifier:@"afterunwind" sender:self];
    }
}

- (IBAction)unwind:(UIStoryboardSegue *)segue
{
    NSLog(@"Unwind");
    _unwindExecuted = YES;
}
like image 180
JAManfredi Avatar answered Dec 24 '22 08:12

JAManfredi