2011-02-18 126 views
1

如何将按钮添加到导航基础的应用程序的导航工具栏中?将按钮添加到导航工具栏

默认情况下,rootController是一个tableView,这是完美的。我想向导航控制器添加一个“共享”按钮,以便我可以将其附加到我自己的方法。

如果导航栏被添加到代码中,这是如何完成的?

回答

4

要做到这一点的代码,去你的观点viewDidLoad方法,并创建一个UIBarButtonItem

此代码将创建一个按钮,上面写着份额就像你想要的,把它的导航栏的右侧。

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithTitle:@"Share"  style:UIBarButtonItemStyleBordered target:self action:@selector(share)]; 

    self.navigationItem.rightBarButtonItem = shareButton; 
    [shareButton release]; 
} 
+0

它应该立即释放吗? – jarryd 2011-02-18 19:28:12

1
UIBarButtonItem *shareButton = [[[UIBarButtonItem alloc] initWithTitle:@"Share" style:UIBarButtonItemStylePlain target:self action:@selector(yourShareAction)] autorelease]; 


self.navigationItem.rightBarButtonItem = shareButton; 
0

这使得一个添加按钮,但你明白了。

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] 
    initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
    target: 
    self action:@selector(buttonPressed:)] 
    autorelease]; 
0

您可以创建UIBarButtonItem并将其附加到根视图控制器的导航项目。您在初始化UIBarButtonItem对象时添加一个目标动作对,并且该动作消息将发送给用户点击该按钮时的目标。

所以如果你想要显示表视图的按钮,将UIBarButtonItem设置为你的表视图控制器的navigationItem.rightBarButtomItem属性。