2013-02-22 24 views

回答

0

下面是簡單的代碼,以便在UINavigationBar

添加UIButton在下面的代碼,您可以添加按鈕使用FlexibleSpace

NSMutableArray *barItems = [[NSMutableArray alloc] init]; 
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 
    [barItems addObject:flexSpace]; 
    [flexSpace release]; 

    UIBarButtonItem *btnFirst = [[UIBarButtonItem alloc] initWithTitle:@"First" style:UIBarButtonItemStyleBordered target:self action:@selector(FirstTapped:)]; 
    [barItems addObject: btnFirst]; 

    UIBarButtonItem *btnSecond = [[UIBarButtonItem alloc] initWithTitle:@"Second" style:UIBarButtonItemStyleBordered target:self action:@selector(SecondTapped:)]; 
    [barItems addObject:btnSecond]; 

UIBarButtonItem *btnThird = [[UIBarButtonItem alloc] initWithTitle:@"Third" style:UIBarButtonItemStyleBordered target:self action:@selector(ThirdTapped:)]; 
    [barItems addObject: btnThird]; 

self.navigationItem.rightBarButtonItems = barItems; 

按鈕相關的方法

-(void) FirstTapped:(UIBarButtonItem *)sender{ 

//perform your action 

} 
-(void) SecondTapped:(UIBarButtonItem *)sender{ 

//perform your action 

} 

-(void) ThirdTapped:(UIBarButtonItem *)sender{ 

//perform your action 

} 

注意:self.navigationItem.rightBarButtonItems只適用於iOS 5或後者。

+0

類似按鈕難道不應該是' - (無效)FirstButtonTapped {// 碼相關的功能 }' ? – 2013-02-22 08:30:09

0

要添加在左邊或右邊,你必須調用下列方法之一多個按鈕:

- (void)setRightBarButtonItems:(NSArray *)items animated:(BOOL)animated 

- (void)setLeftBarButtonItems:(NSArray *)items animated:(BOOL)animated 

數組items包含UIBarButtonItem類的實例。您可以實例化他們這樣

UIBarButtonItem *FirstButton = [[UIBarButtonItem alloc] 
         initWithTitle:@"Title"            
           style:UIBarButtonItemStyleBordered 
           target:self 
           action:@selector(buttonTapped:)]; 

然後,你必須實現選擇buttonTapped:當按鈕被敲擊時調用。

-(void)buttonTapped:(UIBarButtonItem *)button 
{ 
    // do the things that should happen when the button is pressed 
} 

如果你不想讓他們在左側或右側,也可以創建一個UIView自己包含的按鈕和設置視圖是UINavigationItem的titleview的。這種效果是在Facebook的應用程序

[self.navigationItem setTitleView:yourView]; 
0
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

// here is custom button setup // 

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:customButton]; 

[self.navigationItem setLeftBarButtonItem:item animated:YES];