问题很简单。我需要和通常一样的方法--如何用我的方法替换原来的方法,但@selector(presentViewController:animated:completion:)
在iOS9中呢?它只工作在iOS9模拟器,而不是真正的设备。
是的,这段代码被调用了,但是您得到了EXC_BAD_ACCESS
。
为了测试它,我从“主细节模板”中创建了一个测试应用程序,添加了一个按钮并添加了以下代码:
UIViewController *vc = [[UIViewController alloc] init];
[self.navigationController presentViewController:vc animated:YES completion:nil];
更新
@implementation UINavigationController (Extension)
- (void)swizzledPresentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
{
[ self swizzledPresentViewController:viewControllerToPresent animated:flag completion:completion ];
}
#pragma mark -
+ (void)swizzle:(Class)class oldSelector:(SEL)old newSelector:(SEL)new
{
Method oldMethod = class_getInstanceMethod(class, old);
Method newMethod = class_getInstanceMethod(class, new);
if(class_addMethod(class, old, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
{
class_replaceMethod(class, new, method_getImplementation(oldMethod), method_getTypeEncoding(oldMethod));
}
else
{
method_exchangeImplementations(oldMethod, newMethod);
}
}
+ (void)load
{
[self swizzle:UINavigationController.class oldSelector:@selector(presentViewController:animated:completion:) newSelector:@selector(swizzledPresentViewController:animated:completion:)];
}
@end
而且几乎相同的代码都写在NSHipster网站上
更新
警告!这个bug是在非常随机的设备上复制的(在我的例子中,它是iphone5 + ios9.0,但其他用户拥有更新的设备和带有iOS9.1的设备)。
发布于 2015-11-23 09:50:46
https://stackoverflow.com/questions/33737213
复制相似问题