14

I am using a UITableView in my app, which when tapped will use UINavigationController to push another view controller onto the navigation controller's view controller stack.

However, when a cell on the tableview is double †apped, tableView:didSelectRowAtIndexPath: gets called twice, causing the navigation controller to push two of these new viewcontroller s on the stack and resulting in the following console output:

nested pop animation can result in corrupted navigation bar
Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.

It's not a very important issue, because users are not very likely to perform a double tap on the table view cells, however I was wondering if there is an elegant solution to prevent this kind of misnavigation? (maybe checking the navigation controller's topViewController and subsequently decide whether the push pushViewController: method should actually be performed?).

2
  • 1
    Have you played around with [_tableView setMultipleTouchEnabled:(BOOL)]? because by default tableview takes single touch. Commented May 27, 2014 at 12:56
  • I just checked my xib files and indeed the Multiple Touch box was checked for the table view, I can't remember having set them, but this of course explains my issue. I was really wondering why this issue occurred and if table views really would be that "dump". Glad they aren't :)
    – nburk
    Commented May 27, 2014 at 13:00

5 Answers 5

13

Just set userInteractionEnabled to NO like this

in your viewWillAppear

-(void)viewWillAppear:(BOOL)animated
{

  // if you return from another viewController
  [tableView setUserinteractionEnabled:YES];

}

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

  // do your logic here and at the end just set user interaction to NO
  [tableView setUserInteractionEnabled:NO];

}

This will block all user interactions with your tableview.

2
  • How will they enable it for next valid tap? Commented May 27, 2014 at 12:57
  • They wont he said he only needs it once. Or I am getting the question wrong. This tableView should lead user to another viewController, so when this another viewController is dismissed and user is returned to this userInteraection will be re enabled.
    – Pancho
    Commented May 27, 2014 at 12:59
11

Here is one line solution

self.tableView.multipleTouchEnabled = NO;
6
  • It seems very logical to me that this should work, additionally I noticed that I had played around with this property in the past and the Multiple Touch box was checked in interface builder, however, even after removing this property and even specifically calling [self.tableView setMultipleTouchEnabled:NO], the table view still receives both touch events. I added a the following log message directly into tableView:didSelectRowAtIndexPath: NSLog(@"Handles multiple touches: %@", [tableView isMultipleTouchEnabled] ? @"YES" : @"NO"); and the output I received was:
    – nburk
    Commented May 27, 2014 at 13:13
  • Handles multiple touches: NO Handles multiple touches: NO
    – nburk
    Commented May 27, 2014 at 13:14
  • 2 lines of log were printed, when you clicked once?
    – l0gg3r
    Commented May 27, 2014 at 13:16
  • No, sorry it's the output I receive when I double tap. Which I find confusing because I would expect if isMultipleTouchEnabled is set to NO, then I would expect the table view only to receive one touch event.
    – nburk
    Commented May 27, 2014 at 13:43
  • Another solution is to disable user interaction on "didSelectRowAtIndexPath", and enable it after your job (segue to another screen / data fetch / etc...) is done.
    – l0gg3r
    Commented May 27, 2014 at 13:49
7

A fix I've found for the issue described here is wrapping all my didSelectRowAtIndexPath code inside the following check:

    if ([[self navigationController] topViewController] == self) {
        [your existing didSelectRowAtIndexPath code here]
    }

This checks to see whether another view controller has already been pushed to the top of the view controller stack (and is still waiting to actually appear on screen for some reason).

I believe that the multiTouchEnabled property mentioned elsewhere actually refers to the multi-touch feature, which allows iOS to register several touch events simultaneously (for example, pinch-to-zoom using two fingers on the screen at the same time) rather than multiple sequential touches.

0
7

Jordan Sorensen modernized to swift:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    if self.navigationController?.topViewController == self
    {
        self.performSegueWithIdentifier("whatever", sender:self)
    }
    else{
        // uncomment to break in else case print("fasttaper's superfluous input was ignored")
    }
}
0
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    if self.navigationController?.topViewController == self
    {
        return true
    }
    else{
        return false
    }
}

disable multiple touch on your cell

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.