dismissViewControllerAnimated:completion:方法替换不适用于Xcode4.6.1

时间:2013-03-21 12:11:00

标签: ios uiviewcontroller ios4 xcode4.6

我一直在重写“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)
  • 我写了非常简短的示例代码(它只是呈现模态视图,并关闭),但我得到了相同的错误。

我不知道要解决这种情况......请帮忙

1 个答案:

答案 0 :(得分:0)

这是解决方案。 (我使用了错误的功能)

+ (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));

    // 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));
}