UIViewController 和 UIview dealloc 没有被调用

标签 uiview delegates ios4 uiviewcontroller dealloc

我有一个基于导航的 View Controller ,在 View Controller 中我隐藏了顶部导航栏并使用自定义 UIView 作为导航栏。

UIView 栏有一个后退按钮,当按下后退按钮时,我使用委托(delegate)方法(我已经声明了一个协议(protocol))与 View Controller 进行通信。

我在自定义导航栏中使用委托(delegate) id 委托(delegate);

并且在主视图 Controller 中,当我分配导航栏时,我设置了委托(delegate)

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

我在 ViewControllers dealloc 中释放了这个 Bar。

现在,当我按下后退按钮时,我使用委托(delegate)方法在主 ViewController 中调用 popViewController。

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

//In View COntroller
-(void)TopNavigationBarBackButtonPressed {

    [self.navigationController popViewControllerAnimated:YES];
}

现在 ViewController 已弹出,控件转到前一个 viewController,但 ViewController 和 UIView 中均未触发 dealloc

我做错了什么?

最佳答案

好的!终于明白问题是什么了。

是的,是委托(delegate)。因此,在我的“按下后退按钮”方法中,我需要将委托(delegate)设置为 NIL。

-(void)TopNavigationBarBackButtonPressed {

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

瞧,所有的 dealloc 都被调用了。该死的你的自定义协议(protocol)。我生命中的两天我永远不会回来。

编辑: 好的,无需将委托(delegate)设置为零。

我遇到了所有问题,因为在属性(property)中我保留了委托(delegate)。

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

这应该是

@property(assign)id <ASNavigationDelegate> delegate;

关于UIViewController 和 UIview dealloc 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4487840/

相关文章:

ios - UIView的shadowpath颜色没有改变

ios - swift 委托(delegate)不工作

c# - 以字符串作为索引和函数指针或委托(delegate)作为值的 SortedList?和通过字符串调用函数?

objective-c - 'applicationDidBecomeActive' 之后调用了什么方法?

ios - 将 .xib 文件连接到自定义类

ios - 如何将值从 UIView 的子类传递到 UIViewController 类

iOS Delegate 以防止无效输入

c# - 调用函数的最佳方式是什么?

iphone - iPad 中的 UIWebView 内容调整大小问题

iphone - MKOverlay 和 MKAnnotation 有什么区别?