How to disable multi click on button?

孤人 提交于 2019-12-06 04:07:47

问题


I have a UITableView:

Cell 1: Button 1->push to view controller A

Cell 2: Button 2->push to view controller B

It works fine. However, when I try to hold and press two buttons at the same time, my app receives following warning:

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

How should I disable multi click button on cell ?


回答1:


You just need to disable the button while pushing to another View Controller. You can create category ofUINavigationController for pushing to another view Controller. Make sure that you enable the button before coming back to current viewController

@interface UINavigationController (CompletionHandler)

- (void)pushViewController:(UIViewController *)viewController
              animated:(BOOL)animated
            completion:(void (^)(void))completion;

@end




@implementation UINavigationController (CompletionHandler)
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated completion:(void (^)(void))completion {
[CATransaction begin];
[CATransaction setCompletionBlock:completion];
[self pushViewController:viewController animated:animated];
[CATransaction commit];
}


@end

Call the below code for pushing to another ViewController

[self.navigationController pushViewController:ControllerObj animated:YES completion:^{
        btn.userInteractionEnabled = NO; // Your Button Object
        NSLog(@"COMPLETED");
    }];



回答2:


You have to set exclusive touch on each cell's button.

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    // Get your cell

    cell.button.exclusiveTouch = YES;

    return cell;

}



回答3:


if above method is not working then when one button is clicked you can disable the interaction with the cell

cell.userInteractionEnabled = NO;

Later as per your need or in viewWillAppear enable the same.




回答4:


cell.contentView.exclusiveTouch = YES;
cell.exclusiveTouch = YES;

Also disable the multiple touch property from the tableView

self.tableView.multipleTouchEnabled = NO;



回答5:


Change your button's IBAction forControllEvent as UIControlEventTouchDown.

if you use storyboard cell hook up your buttons action by changeing touchUpInside to touchDown.

or

[YOUR_BUTTON addTarget:self action:@selector(YOUR_IBACTION:) forControlEvents:UIControlEventTouchDown];

This will prevent button touch and holding. and this Action will call push immediatly at your touch.



来源:https://stackoverflow.com/questions/30251663/how-to-disable-multi-click-on-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!