Frage

I don't know how to solve this problem.

I have a Controller (ControllerA) with a xib. Inside that xib i have a custom UIView (UIViewA). UIViewA is in all of my other xibs as a footer and is loaded with awakeFromNib.

The code of the initialization is the following:

-(void)awakeFromNib

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

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

and in the dealloc:

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

The problem comes when I navigate to another controller and I return back it gives me a deallocation exception in the UIViewA view. What I have seen is that when I return back to my first controller dealloc is called and the observer is removed... Is there a way to initialize the Notifications when the view appears again?.

I don't know if I have explained very well.

Thank you in advance.

War es hilfreich?

Lösung

This line:

[[NSNotificationCenter defaultCenter] removeObserver:@"downloadNotification"];

should be:

[[NSNotificationCenter defaultCenter] removeObserver:self];

because the parameter is the object to remove as an observer, not the name of the notification.

This error will result in the deallocated view not being removed as an observer so you will get a crash the next time a notification is posted.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top