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?

B K
  • 1,534
  • 2
  • 18
  • 38
  • 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!!! – B K Dec 21 '10 at 06:43

1 Answers1

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;
Adil Soomro
  • 36,617
  • 9
  • 98
  • 146
B K
  • 1,534
  • 2
  • 18
  • 38
  • 1
    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! – Slee Dec 19 '11 at 16:25
  • overriding retain/release and looking at the call stack can also be helpful for these situations. – markshiz Feb 08 '12 at 23:13