UIViewController & UIview dealloc not getting called

B K picture B K · Dec 20, 2010 · Viewed 8k times · Source

I have a Navigation based view controller and in the view controller i have hidden the top navigation bar and use a custom UIView as the navigation bar.

The UIView bar has a back button and I use Delegate methods(I have declared a protocol) to communicate to the view controller when the back button is preesed.

I use delegate in my CustomNavigation Bar id delegate;

and In the main View Controller when i allocate the navigation bar i set the delegate

topBar = [[TopNavigationBar alloc] initWithFrame:CGRectMake(0, 0, 480, 40)];
topBar.lblTitle.text = @"Shop";
topBar.delegate = self;

I release this Bar in the ViewControllers dealloc.

Now when i press the back button I use delegate method to call a popViewController in the main ViewController.

//in Custom Bar
-(void)ButtonPressed {
    [delegate TopNavigationBarBackButtonPressed];   
}

//In View COntroller
-(void)TopNavigationBarBackButtonPressed {

    [self.navigationController popViewControllerAnimated:YES];
}

Now the ViewController is poped and the control goes to the previous viewController but the dealloc is not fired in both the ViewController and the UIView

What am i doing wrong?

Answer

B K picture B K · Dec 23, 2010

OK! Finally understood what the problem was.

Yes it was the delegate. So in my "back button pressed" method, I need to set the delegate to NIL.

-(void)TopNavigationBarBackButtonPressed {

 topBar.delegate = nil;
[self.navigationController popViewControllerAnimated:YES];
}

And voila, all the dealloc get called. Damn you custom Protocol. 2 Days of my life i will never get back.

EDIT: OK no need to set the delegate to nil.

I was having all the problems because in the property i was retaining the delegate.

@property(nonatomic, retain)id <ASNavigationDelegate>delegate;

This should be

@property(assign)id <ASNavigationDelegate> delegate;