MichalMoskala MichalMoskala - 3 months ago 25
iOS Question

How to prevent performing the same segue twice?

I've got a problem with navigation in my iOS app. I have sliding menu with table view of menu items. Tapping on one of those item fires segue which pushes new view controller on the stack. The problem is that I can push the same view controller twice on the same stack. If I select menu item "A" then corresponding view controller is pushed on navigation stack. From this newly pushed view controller I also have an option to select the same "A" menu item and push new istance of the same class as topmost view controller. How can I prevent that?

Not working solutions:

Checking the class of the top controller of navigation stack. But the problem is that I only knows the segue's identifier (NSString* object) and cannot get the information about the destination view controller class. The only place where I can check that class is method:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender


but I cannot prevent performing the segue from this method.

I have tried saving last performed segue in property but it won't work because when I push "A" view controller, then "B" view controller, then press back button I won't be able to push B again which is wrong :)

Answer

you need the following method

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender 

just return false to cancel the segue. You will have something like this(pseudocode):

if ([performedSeguesArray contain:identifier])
{
     return NO;
}
else
{
    [performedSeguesArray addObject:identifier];
}

return YES;