问题
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