首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >目标C、iOS 9的先进方法?

目标C、iOS 9的先进方法?
EN

Stack Overflow用户
提问于 2015-11-16 14:01:38
回答 1查看 693关注 0票数 4

问题很简单。我需要和通常一样的方法--如何用我的方法替换原来的方法,但@selector(presentViewController:animated:completion:)在iOS9中呢?它只工作在iOS9模拟器,而不是真正的设备。

是的,这段代码被调用了,但是您得到了EXC_BAD_ACCESS

为了测试它,我从“主细节模板”中创建了一个测试应用程序,添加了一个按钮并添加了以下代码:

代码语言:javascript
运行
复制
UIViewController *vc = [[UIViewController alloc] init];
[self.navigationController presentViewController:vc animated:YES completion:nil];

更新

代码语言:javascript
运行
复制
@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的设备)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-23 09:50:46

问题是在检查nil值时会出现断点。这种情况的解决办法:

1)检查块是否为nil,然后用空的非零块替换它。

2)使用现成库。我试过:https://github.com/rabovik/RSSwizzle

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33737213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档