我正在尝试向UIBarButtonItem
我的导航项添加自定义。这个按钮将在我所有的视图控制器中可用。因此,我没有将它添加到每个viewDidLoad
方法中,viewDidLoad
而是尝试将它添加到我的应用程序 Delegate 类中的一个单独的方法中,我将在该方法中调用该viewDidLoad
方法。这是我的代码:
UIImage* image = [UIImage imageNamed:@"some-header-icon.png"];
CGRect frame = CGRectMake(0,0,image.size.width,image.size.height);
UIButton* hellBtn = [[UIButton alloc] initWithFrame:frame];
[hellBtn setBackgroundImage:image forState:UIControlStateNormal];
[hellBtn setShowsTouchWhenHighlighted:NO];
[hellBtn addTarget:self action:@selector(goToHell) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:hellBtn];
[self.nav.navigationController.navigationItem setRightBarButtonItem:rightBarButtonItem];
[hellBtn release];
[rightBarButtonItem release];
如果我在任何 viewController 中替换相同的代码博客viewDidLoad
并更改
[self.nav.navigationController.navigationItem setRightBarButtonItem:rightBarButtonItem];
经过:
[self.navigationItem setRightBarButtonItem:rightBarButtonItem];
它完美地工作。任何想法为什么?