【发布时间】:2013-03-21 12:11:00
【问题描述】:
我一直在覆盖“dismissViewControllerAnimated:completion:”以在 iOS4.3 中使用此方法。 但是自从我更新 Xcode(4.6.1) 以来,这种技术出现了运行时错误
error: address doesn't contain a section that points to a section in a object file
神秘的是,“presentViewController:animated:completion:”方法没有问题...
这里是我使用的“UIViewController+Compatibility.h”。
@implementation UIViewController (Compatibility)
- (void)iOS4_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
{
[self presentModalViewController:viewControllerToPresent animated:flag];
[self performSelector:@selector(callBlock:) withObject:completion afterDelay:(flag ? 0.5 : 0)];
}
- (void)iOS4_dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
[self dismissModalViewControllerAnimated:flag];
[self performSelector:@selector(callBlock:) withObject:completion afterDelay:(flag ? 0.5 : 0)];
}
- (void)callBlock:(void (^)(void))block
{
if (block) {
block();
}
}
+ (void)iOS4compatibilize
{
// presentViewController:animated:completion:
Method m1 = class_getInstanceMethod(self.class, @selector(iOS4_presentViewController:animated:completion:));
class_addMethod(self.class,
@selector(presentViewController:animated:completion:),
method_getImplementation(m1),
method_getTypeEncoding(m1));
// MEMO:
// This cause a bug.
// Method m2 = class_getClassMethod(self.class, @selector(iOS4_dismissViewControllerAnimated:completion:));
// Here is the answer.
// dismissViewControllerAnimated:completion:
Method m2 = class_getInstanceMethod(self.class, @selector(iOS4_dismissViewControllerAnimated:completion:));
class_addMethod(self.class,
@selector(dismissViewControllerAnimated:completion:),
method_getImplementation(m2),
method_getTypeEncoding(m2));
}
@end
有人遇到同样的问题吗?
备忘录:这些我都试过了。
- 清理我预先编译的二进制文件。
- 重新启动 Xcode,或重新启动我的 Mac。
- 我的 Xcode 是当前最新版本(版本 4.6.1)
- 我编写了非常短的示例代码(它只是显示模态视图,然后关闭),但我遇到了同样的错误。
我不知道如何解决这种情况...请帮助
【问题讨论】:
-
我很惊讶它的工作,你不认为
dismissModalViewControllerAnimated释放视图控制器(也许不是直接)?我很想看看以下内容是否有帮助:在iOS4_dismissViewControllerAnimated和iOS4_presentViewController保留self,在callBlock发布。不过,这并不是一个好的解决方案。 -
@A-Live 感谢您的评论!我在我的代码中发现了一个简单的错误。我正在使用“class_getClassMethod”(我的意思是“class_getInstanceMethod”),并且在 m2 上得到了 nil ......现在它可以工作了!
-
很好,我也没有注意到,请将解决方案作为答案发布(当然接受),以便将问题标记为已解决。
标签: ios uiviewcontroller ios4 xcode4.6