解决TTThumbsViewController设置delegate为当前class导致回退按钮消失

今天在做一个照片墙的功能的时候突然发现Three20的一个问题:

我的一个ViewController继承自TTThumbsViewController,这个Controller是由一个UINavigationController通过pushViewController: animated: 方法push出来的。

一开始,一切正常。但是后来我突然间发现UINavigationController的导航按钮消失不见了。

因为在最开始的时候我并没有发现这个问题,直到我把这个功能做的大概七七八八后,在过一次流程的时候才突然发现这个的。

发现UINavigationController的导航按钮消失后,通过注释代码的方法发现了当把TTThumbsViewController的delegate设置为self的时候,导航按钮就消失了。

一开始不得其门道,想不通为嘛当把delegate设置为self后会发生这样的问题,在google半天未见结果的后,决定看下TTThumbsViewController的实现。

在TTThumbsViewController的implementation中发现了以下代码:

- (void)setDelegate:(id)delegate {
  _delegate = delegate;

  if (_delegate) {
    self.navigationItem.leftBarButtonItem =
    [[[UIBarButtonItem alloc] initWithCustomView:[[[UIView alloc] init]
                                                  autorelease]]
     autorelease];

    self.navigationItem.rightBarButtonItem =
    [[[UIBarButtonItem alloc] initWithTitle:TTLocalizedString(@"Done", @"")
                                      style:UIBarButtonItemStyleBordered
                                     target:self
                                     action:@selector(removeFromSupercontroller)] autorelease];
  }
}

看到这里就完全明白为什么UINavigationBar的导航按钮会消失不见了,那么要解决这个问题也就简单了,初步想到可以有两种解决方法:

  1. 注释掉self.navigationItem.leftBarButtonItem =
    [[[UIBarButtonItem alloc] initWithCustomView:[[[UIView alloc] init]
    autorelease]]
    autorelease];这句。
  2. 把TTThumbsViewController的delegate设置为一个其他的class。

因为我的很多代码直接写到了Controller中,所以我就直接采用了第一种方法了。注释掉那句代码后,可爱的UINavigationBar的导航按钮又回来了。

–EOF

2 Responses to “解决TTThumbsViewController设置delegate为当前class导致回退按钮消失”

  1. Good post, have a great Christmas!

  2. Websites you should visit…

    [...]below you’ll find the link to some sites that we think you should visit[...]……

Leave a Reply