gpt4 book ai didi

objective-c - 将 UIBarButtonItem 添加到 UINav..Controller

转载 作者:行者123 更新时间:2023-12-03 20:17:49 25 4
gpt4 key购买 nike

我不确定我在这里缺少什么。我有一个自定义 UINavigationController 并且我正在尝试将持久的 UIBarButtonItem 添加到栏。

-(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。

更新:目前这是我能得到的最接近的:

-(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 中......如果我尝试使用现有的,我会收到此错误:

'NSInternalInconsistencyException', reason: 'Cannot call pushNavigationItem:animated: directly on a UINavigationBar managed by a controller.'

……真的吗???

最佳答案

navigationItem 不得在 UINavigationController 实例上设置,而应在显示在导航 Controller “内部”的 view 的 View Controller 上设置.

如果您的 Controller 本身被插入另一个导航 Controller ,则在导航 Controller 上设置 self.navigationItem 将会起作用。

关于objective-c - 将 UIBarButtonItem 添加到 UINav..Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2520912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com