ios - 单击按钮快速推送 UIViewController 两次

标签 ios iphone swift uinavigationcontroller pushviewcontroller

示例:

我在当前 View 中有两个按钮,当我单击按钮时,它将在当前 UINavgitionController 中推送一个 UIViewController

这是我的代码:

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

但是我发现了一个错误。当我快速单击按钮时,UIViewController 会按两次,为什么会发生这种情况?

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

最佳答案

我也遇到了同样的问题,我通过创建仅需要独占触摸的 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];
    }

关于ios - 单击按钮快速推送 UIViewController 两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33431698/

相关文章:

swift - RxSwift 正确的方式

arrays - 将整个数组从 int 转换为 double 以进行一些算术运算

ios - 带有 Swift 的 Chromecast objectForKey

ios - 单行 UITableView 在滚动之前不显示 detailTextLabel

iOS NSURLSessionUploadTask 响应数据

iphone - 为什么必须填充 UITextField 才能使 UIButton 作为 subview 消失

ios - 带有 UIView 的自动布局标签和 UIView 内的标签

iphone - 使 UINavigationBar 半透明

ios - CvVideoCamera - 相机不可用,Iphone 模拟器

ios - 通过点击 X 按钮识别 AVPlayerViewController 何时关闭