Observing UIView changes crash

China☆狼群 提交于 2019-12-13 14:51:39

问题


i've used the following code below to determine when views get added to a superview:

//Makes views announce their change of superviews
Method method = class_getInstanceMethod([UIView class], @selector(willMoveToSuperview:));
IMP originalImp = method_getImplementation(method);

void (^ block)(id, UIView *) = ^(id _self, UIView *superview) {
    [_self willChangeValueForKey:@"superview"];
    originalImp(_self, @selector(willMoveToSuperview:), superview);
    [_self didChangeValueForKey:@"superview"];
};

IMP newImp = imp_implementationWithBlock((__bridge id)((__bridge void*)block));
method_setImplementation(method, newImp);

i haven't had any issues with this, but when i try to run it in 64-bit, i get

EXC_BAD_ACCESS (code=EXC_I386_GPFLT) on originalImp(_self, @selector(willMoveToSuperview:), superview);

anyone have any insight?

thanks


回答1:


It seems like using an imp_implementationWithBlock crashes on arm64.

You may try classic swizzling instead (put the code in your block in a dedicated swizzledWillMoveToSuperview: method, exchanging implementations of the two methods / selectors, and call [self swizzledWillModeToSuperview:superview] in swizzledWillModeToSuperview: to actually call the original implementation).

Avoiding using imp_implementationWithBlock and using an implementation of an existing method instead seems to work for both 32 and 64 bit architectures.

Be aware that this won't cover all the cases anyway, especially if someone subclasses an UIView and override willMoveToSuperview without calling super, then those implementations won't be swizzled as you expect and the observation on these won't work.



来源:https://stackoverflow.com/questions/22567869/observing-uiview-changes-crash

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