Search code examples
iostouchtransitionuistoryboardsegue

iOS: Disable all touches for some time after calling 'prepareForSegue:'


The problem is that user can tap two buttons that perform push segues in one moment, and in this case two view controllers are pushed one after another. Here is a code piece:

- (void)cellTapped:(MyCell *)cell
{
    NSLog(@"cell tapped %p", cell);
    [self performSegueWithIdentifier:@"MySegue" sender:cell];
}
...
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSLog(@"prepareForSegue: %@", [segue identifier]);
}

Console log when I tap immediately two buttons:

2014-05-22 18:33:09:622 zzz[1737:1547] cell tapped 0x17558e10
2014-05-22 18:33:09:631 zzz[1737:1547] prepareForSegue: MySegue
2014-05-22 18:33:09:873 zzz[1737:1547] cell tapped 0x17554720
2014-05-22 18:33:09:875 zzz[1737:1547] prepareForSegue: MySegue

So looks like it's a bug. I would like to disable touches on screen while segue is performing transition.


Solution

  • UIApplication offers you beginIgnoringInteractionEvents and endIgnoringInteractionEvents so you can disable all touch event handling during certain times (like a view transition).

    Be sure to call endIgnoringInteractionEvents once for each time you call beginIgnoringInteractionEvents.