如何在iPhone的导航栏中添加右键控制按钮 [英] How to add a right bar button to a navigation bar in iphone

查看:147
本文介绍了如何在iPhone的导航栏中添加右键控制按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在导航栏中添加一个右栏按钮项,这样点击它就可以执行某项功能。

I want to add a right bar button item to the navigation bar, so that on click, it performs certain a function.

我创建了以下代码添加正确的栏按钮项,但完成后,栏按钮项目不会显示在导航栏中:

I have created the following code to add the right bar button item, but after it is done, the bar button item is not getting displayed in navigation bar:

-(void)viewDidload{
    self.navigationItem.rightBarButtonItem = 
    [[[UIBarButtonItem alloc] 
           initWithBarButtonSystemItem:UIBarButtonSystemItemAdd                                                                                                     
                                target:self
                                action:@selector(Add:)] autorelease];    
}

-(IBAction)Add:(id)sender
{
    TAddNewJourney *j=[[TAddNewJourney alloc]init];
    [app.navigationController pushViewController:j animated:YES];
    [j release];
}


推荐答案

-(void)viewDidLoad
{ 
    [super viewDidLoad];
    app.navigationController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(Add:)] autorelease];
}

-(IBAction)Add:(id)sender
{
    TAddNewJourney *j=[[TAddNewJourney alloc]init];
    [app.navigationController pushViewController:j animated:YES];
    [j release];
}

尝试其他答案。我发布了这个答案,如果你的viewcontroller没有我认为是问题的导航控制器,它会起作用。

Try the other answers. I posted this answer so that it will work if your viewcontroller does not have a navigation controller which i think is the problem.

这篇关于如何在iPhone的导航栏中添加右键控制按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆