单击按钮快速按两次UIViewController

时间:2015-10-30 08:37:27

标签: ios iphone swift uinavigationcontroller pushviewcontroller

示例:

我在当前视图中有两个按钮,当我点击该按钮时,它会在当前UIViewController中推送UINavgitionController

这是我的代码:

dispatch_async(dispatch_get_main_queue(), { () -> Void in
    self.navigationController?.pushViewController(pvc, animated: true)
})

但我发现了一个错误。当我快速点击按钮时,UIViewController推两次,为什么会这样?

PS:现在,我已经在很多UIViewController s中使用了这个代码,超过200次。

1 个答案:

答案 0 :(得分:0)

我也遇到了同样的问题,我通过创建UIButton类别解决了这个问题,只会采取独家触摸。

创建UIButton的类别

#import <objc/runtime.h> //Please import

    @implementation UIButton (ExclusiveTouch)

    + (void)load
    {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            Class class = [self class];

            SEL originalSelector = @selector(willMoveToSuperview:);
            SEL swizzledSelector = @selector(inc_willMoveToSuperview:);

            Method originalMethod = class_getInstanceMethod(class, originalSelector);
            Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

            BOOL didAddMethod =
            class_addMethod(class,
                            originalSelector,
                            method_getImplementation(swizzledMethod),
                            method_getTypeEncoding(swizzledMethod));

            if (didAddMethod) {
                class_replaceMethod(class,
                                    swizzledSelector,
                                    method_getImplementation(originalMethod),
                                    method_getTypeEncoding(originalMethod));
            } else {
                method_exchangeImplementations(originalMethod, swizzledMethod);
            }
        });
    }

    - (void)inc_willMoveToSuperview:(UIView *)newSuperview
    {
        // This is correct and does not cause an infinite loop!
        // See the link for an explanation
        [self inc_willMoveToSuperview:newSuperview];

        [self setExclusiveTouch:YES];
    }