OGeek|极客世界-中国程序员成长平台

标题: ios - xib通知dealloc错误中的自定义UIView [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:34
标题: ios - xib通知dealloc错误中的自定义UIView

我不知道如何解决这个问题。

我有一个带有 xib 的 Controller (ControllerA)。 在那个xib里面我有一个自定义的UIView(UIViewA)。 UIViewA 在我所有的其他 xib 中作为页脚并加载了 awakeFromNib。

初始化代码如下:

-(void)awakeFromNib

{
    [[NSBundle mainBundle] loadNibNamed"ICOMFooterView" owner:self options:nil];
    [self addSubview:self.footerView];

    [[NSNotificationCenter defaultCenter] addObserver:self selectorselector(receivedNotification name"downloadNotification" object:nil];
    [self checkIsDownloading];
}

在dealloc中:

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver"downloadNotification"];
}

当我导航到另一个 Controller 并返回时出现问题,它在 UIViewA View 中给了我一个释放异常。我所看到的是,当我返回到我的第一个 Controller 时,会调用 dealloc 并删除观察者......有没有办法在 View 再次出现时初始化通知?

不知道我解释的好不好。

提前谢谢你。



Best Answer-推荐答案


这一行:

[[NSNotificationCenter defaultCenter] removeObserver"downloadNotification"];

应该是:

[[NSNotificationCenter defaultCenter] removeObserver:self];

因为参数是作为观察者要移除的对象,而不是通知的名称。

这个错误会导致被释放的 View 不会作为观察者被移除,所以下次发布通知时会崩溃。

关于ios - xib通知dealloc错误中的自定义UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23338675/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4