MichalMoskala
MichalMoskala

Reputation: 927

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 :)

Upvotes: 1

Views: 2885

Answers (2)

DEV
DEV

Reputation: 949

I had the same issue.It is resolved now. Please let me know if this helps.

I already have a segue linked from table view cell to another view with name DynamicSuperView

func tableView(_tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{
//Remove the below line from this function as it will perform segue.
//performSegue(withIdentifier: "DynamicSegue", sender: self)
}


override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?)         {
// This will actually perform your segue
var DestViewController = segue.destination as! DynamicSuperView 
let selectedRow = tableView.indexPathForSelectedRow?.row
DestViewController.labelText = names[selectedRow!]
}

Upvotes: 1

Igor
Igor

Reputation: 1557

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;

Upvotes: 2

Related Questions