NSNotificationCenter添加或删除UIContentSizeCategoryDidChangeNotification

时间:2015-01-27 18:49:58

标签: objective-c

将NSNotificationCenter -addObserver:selector:name:object:-removeObserver:name:object:UIContentSizeCategoryDidChangeNotification一起使用时,我在iOS 6上收到EXEC_BAD_ACCESS code=1, address=0x0

我在-viewWillAppear:-viewWillDisappear:

中使用了以下内容
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIContentSizeCategoryDidChangeNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(preferredFontSizeChanged:) name:UIContentSizeCategoryDidChangeNotification object:nil];
    }

    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIContentSizeCategoryDidChangeNotification object:nil];
    }

此外,请注意UIContentSizeCategoryDidChangeNotification声明为:

    UIKIT_EXTERN NSString *const UIContentSizeCategoryDidChangeNotification NS_AVAILABLE_IOS(7_0);

哪个解析为:

    #define UIKIT_EXTERN extern __attribute__((visibility ("default")))

1 个答案:

答案 0 :(得分:1)

通常,iOS SDK中的常量声明为extern NSStringextern关键字是一个合同,声明此内存将在另一个编译单元中分配。因此,在iOS 6中,该通知类型不存在,因此错误访问是由于未分配字符串。