将 UIBarButtonItem 添加到 UINav..Controller

发布于 2024-08-26 19:43:19 字数 1562 浏览 2 评论 0原文

我不确定我在这里缺少什么。我有一个自定义 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.'

....真的吗???

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");
}

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];
}

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???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏璃陌 2024-09-02 19:43:19

navigationItem 不得在 UINavigationController 实例上设置,而应在显示在导航控制器“内部”的 view 的视图控制器上设置。

如果您的控制器本身被推入另一个导航控制器,则在导航控制器上设置 self.navigationItem 会起作用。

navigationItem must not be set on the UINavigationController instance but on the view controller of the view which is displayed "inside" the navigation controller.

Setting self.navigationItem on your navigation controller would work if your controller was itself pushed into another navigation controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文