【发布时间】:2012-07-03 11:42:54
【问题描述】:
我正在尝试将自定义 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];
完美运行。 知道为什么吗?
【问题讨论】:
-
什么是导航?它是一个视图控制器吗?
-
nav 是我的 UINavigationController
标签: objective-c ios uinavigationcontroller uiapplicationdelegate