2011-02-18 121 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屬性。