示例:
我在当前 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/