方法Swizzling在iOS 8中不起作用?

时间:2014-10-24 07:20:23

标签: ios ios8 xcode6

我在我的项目中采用了Mattt Thompson Method Swizzling代码(感谢Mattt Thompson!),它在iOS 7及更早版本中运行良好,但在iOS 8中,似乎这种方法调整不起作用此外,从不调用deleteBackwardSwizzle,代码如下:

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    Class class = [self class];
    // When swizzling a class method, use the following:
    // Class class = object_getClass((id)self);
    SEL originalSelector = @selector(deleteBackward);
    SEL swizzledSelector = @selector(deleteBackwardSwizzle);

    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);
    }
});

1 个答案:

答案 0 :(得分:0)

经过对这个deleteBackward方法的进一步研究,我发现它与调配无关。我按照detect-backspace-in-uitextfield-in-ios8的说明进行操作,一切顺利!