将UIBarButtonItem添加到UINav..Controller [英] Adding UIBarButtonItem to UINav..Controller
问题描述
我不确定我在这里缺少什么。我有一个自定义的 UINavigationController
,我正在尝试向栏中添加一个持久的 UIBarButtonItem
。
i am not sure what i am missing here. I Have a custom UINavigationController
and i am trying to add a persistant UIBarButtonItem
to the bar.
-(void)viewDidLoad
{
self.navigationBar.barStyle = UIBarStyleBlack;
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithTitle:@"Nope..."
style:UIBarButtonItemStyleBordered
target:self
action:@selector(goBack:)];
self.navigationItem.leftBarButtonItem =bbi;
[bbi release];
}
-(void)goBack:(id)sender
{
NSLog(@"go back now");
}
我在这里缺少什么? - 顺便说一句,我不想/不会使用IB。
what am i missing here? - BTW, i do not want to/ will not use IB.
更新:
目前这是我能得到的最接近的:
UPDATE: Currently this is the closest i can get:
-(void)viewDidLoad
{
self.navigationBar.barStyle = UIBarStyleBlack;
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)];
navBar.barStyle = UIBarStyleBlack;
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"Currently Playing..."];
[navBar pushNavigationItem:navItem animated:NO];
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStyleBordered target:self action:@selector(goBack:)];
navItem.rightBarButtonItem = editButton;
[self.view addSubview:navBar];
[editButton release];
[navItem release];
[navBar release];
[super viewDidLoad];
}
我必须将整个导航栏添加到 UINavigationController
已经有一个导航栏....如果我尝试使用现有的,我会收到此错误:
It's terrible i have to add an entire navbar to a UINavigationController
that already has a navbar....if i try to use the existing, i get this error:
'NSInternalInconsistencyException', reason: 'Cannot call pushNavigationItem:animated: directly on a UINavigationBar managed by a controller.'
....真的???
....really???
推荐答案
navigationItem
不得在UINavigationController实例上设置,但在视图上在导航控制器内部显示的视图的控制器。
navigationItem
must not be set on the UINavigationController instance but on the view controller of the view which is displayed "inside" the navigation controller.
如果您的控制器本身被推入另一个导航控制器,则在导航控制器上设置self.navigationItem将起作用。
Setting self.navigationItem on your navigation controller would work if your controller was itself pushed into another navigation controller.
这篇关于将UIBarButtonItem添加到UINav..Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!