7

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

UIView 栏有一个后退按钮,当后退按钮被预置时,我使用委托方法(我已经声明了一个协议)与视图控制器进行通信。

我在我的 CustomNavigation Bar id 委托中使用委托;

并在主视图控制器中,当我分配导航栏时,我设置了委托

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

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

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

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

//In View COntroller
-(void)TopNavigationBarBackButtonPressed {

    [self.navigationController popViewControllerAnimated:YES];
}

现在 ViewController 被弹出并且控制转到前一个 viewController 但在 ViewController 和 UIView 中都没有触发 dealloc

我究竟做错了什么?

4

1 回答 1

17

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

是的,是代表。所以在我的“按下后退按钮”方法中,我需要将委托设置为 NIL。

-(void)TopNavigationBarBackButtonPressed {

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

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

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

我遇到了所有问题,因为在财产中我保留了代表。

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

这应该是

@property(assign)id <ASNavigationDelegate> delegate;
于 2010-12-23T07:47:19.530 回答