UIViewController & UIview dealloc not getting called

混江龙づ霸主 提交于 2019-12-22 04:42:54

问题


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?


回答1:


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;


来源:https://stackoverflow.com/questions/4487840/uiviewcontroller-uiview-dealloc-not-getting-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!