UIViewController & UIview dealloc not getting called
Asked Answered
S

1

7

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?

Silvie answered 20/12, 2010 at 8:4 Comment(1)
any idea people?? A dealloc wont be called if the retainCount is not 0. But i am not retaining the viewController anywhere. I do a [vewController release] after i push it on the stack. So the only reason i can think of is the custom protocol i have written. Do i have to release it? I do retain the delegate in the property. But even if i do release it in the dealloc, it is not getting called. STUMPED!!!Silvie
S
17

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;
Silvie answered 23/12, 2010 at 7:47 Comment(2)
I had a bunch of views that were not releasing because of this, one bad piece of code I wrote copied over many times - thanks for this!Imbed
overriding retain/release and looking at the call stack can also be helpful for these situations.Stig

© 2022 - 2024 — McMap. All rights reserved.