0

我正在使用拆分視圖控制器,其中有2個視圖控制器在第二個視圖控制器我想在導航控制器的右側添加兩個按鈕。 我用下面的代碼添加一個按鈕,它的工作原理:如何在導航欄上添加2個按鈕?

UIBarButtonItem *barButton=[[UIBarButtonItem alloc] init]; 
    [barButton setCustomView:btnShare]; 
    self.navigationItem.rightBarButtonItem=barButton; 

試過此鏈接http://osmorphis.blogspot.in/2009/05/multiple-buttons-on-navigation-bar.html 但不能succeed.Please幫我解決這個問題。

回答

6

試試這個

NSMutableArray *arrRightBarItems = [[NSMutableArray alloc] init]; 
UIButton *btnSetting = [UIButton buttonWithType:UIButtonTypeCustom]; 
[btnSetting setImage:[UIImage imageNamed:@"settings.png"] forState:UIControlStateNormal]; 
btnSetting.frame = CGRectMake(0, 0, 32, 32); 
btnSetting.showsTouchWhenHighlighted=YES; 
[btnSetting addTarget:self action:@selector(onSettings:) forControlEvents:UIControlEventTouchUpInside]; 
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnSetting]; 
[arrRightBarItems addObject:barButtonItem]; 

UIButton *btnLib = [UIButton buttonWithType:UIButtonTypeCustom]; 
[btnLib setImage:[UIImage imageNamed:@"library.png"] forState:UIControlStateNormal]; 
btnLib.frame = CGRectMake(0, 0, 32, 32); 
btnLib.showsTouchWhenHighlighted=YES; 
[btnLib addTarget:self action:@selector(onMyLibrary:) forControlEvents:UIControlEventTouchUpInside]; 
UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:btnLib]; 
[arrRightBarItems addObject:barButtonItem2]; 

UIButton *btnRefresh = [UIButton buttonWithType:UIButtonTypeCustom]; 
[btnRefresh setImage:[UIImage imageNamed:@"refresh.png"] forState:UIControlStateNormal]; 
btnRefresh.frame = CGRectMake(0, 0, 32, 32); 
btnRefresh.showsTouchWhenHighlighted=YES; 
[btnRefresh addTarget:self action:@selector(onRefreshBtn:) forControlEvents:UIControlEventTouchUpInside]; 
UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithCustomView:btnRefresh]; 

[arrRightBarItems addObject:barButtonItem1]; 
self.navigationItem.rightBarButtonItems=arrRightBarItems; 
+0

非常感謝這個工作 – user2050884 2013-03-13 13:23:07

+0

歡迎use​​r2050884,如果它是有用的投票了 – Ravindhiran 2013-03-13 13:25:32

1

試試這個

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
    style:UIBarButtonItemStyleDone target:nil action:nil]; 
self.navigationItem.rightBarButtonItem = rightButton; 
[rightButton release]; 
1

只需使用UISegmentedControl代替UIButton

然後將模式更改爲「瞬間」並在更改上添加操作。

當動作被觸發時,檢查selectedSegmentIndex以知道選擇了哪個段。

1

使UIView添加你想要的按鈕。然後用

UIBarButtonItem *myBarbtn=[[UIBarButtonItem alloc] initWithCustomView:buttonsView]; 
self.navigationItem.rightBarButtonItem = myBarbtn; 
3
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:[GlobalMethods buttonWithImage:@"btn_home" heighlightImageName:@"btn_home_h" buttonFrame:CGRectMake(2, 1, 34, 34) selectorName:@selector(buttonHomeClicked:) target:self]]]; 

    UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [tempButton setFrame:CGRectMake(240, 5, 34, 34)]; 
    [tempButton addTarget:target action:selectorName forControlEvents:UIControlEventTouchUpInside]; 
    [tempButton setImage:[self getImageFromResource:normalImageName] forState:UIControlStateNormal]; 
    [tempButton setImage:[self getImageFromResource:heighlightImageName] forState:UIControlStateHighlighted]; 
    [tempButton setImage:[self getImageFromResource:@"btn_fav_h"] forState:UIControlStateSelected]; 
    [self.navigationController.navigationBar addSubview:self.buttonFavorite]; 
3

創建兩個的UIBarButtonItem和陣列中的同時添加,然後在導航欄中

UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAttachmentClicked:)]; 
UIBarButtonItem *sendButton = [[UIBarButtonItem alloc] initWithTitle:LS(@"Send") style:UIBarButtonItemStyleBordered target:self action:@selector(sendClicked:)]; 
self.navigationItem.rightBarButtonItems = @[addButton,sendButton]; 

增加整個陣列嘗試THI代碼

1

使用分段控制這個

UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray array]]; 
[segmentedControl setMomentary:YES]; 
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"sample1.png"] atIndex:0 animated:NO]; 
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"sample2.png"] atIndex:1 animated:NO]; 
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; 
[segmentedControl addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged]; 

UIBarButtonItem * segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView: segmentedControl]; 
self.navigationItem.rightBarButtonItem = segmentBarItem; 
1

創建一個0的數組個S和把它傳遞給的UINavigationItem

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

方法。

2
UIButton *btnLogOut = [UIButton buttonWithType:UIButtonTypeCustom]; 
    btnLogOut.frame = CGRectMake(0, 0, 62, 31); 
    [btnLogOut setImage:[UIImage imageNamed:@"logout_new.png"] forState:UIControlStateNormal]; 
    [btnLogOut addTarget:self action:@selector(logoutButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

    UIBarButtonItem *logOutBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnLogOut]; 

    UIButton *btnError = [UIButton buttonWithType:UIButtonTypeCustom]; 
    btnError.frame = CGRectMake(0, 0, 62, 31); 
    [btnError setImage:[UIImage imageNamed:@"list-icon.png"] forState:UIControlStateNormal]; 
    [btnError addTarget:self action:@selector(logoutButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 


    UIBarButtonItem *errorButton = [[UIBarButtonItem alloc] initWithCustomView:btnError]; 


    NSArray *navigationBarBtnArray=[NSArray arrayWithObjects:errorButton,logOutBarButton, nil]; 



    self.navigationItem.rightBarButtonItems=navigationBarBtnArray;