0

我正在使用拆分视图控制器,它在第二个视图控制器上有 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 但不能成功。请帮我解决这个问题。

4

9 回答 9

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;
于 2013-03-13T07:37:39.467 回答
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];
于 2013-03-13T07:24:54.780 回答
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];

试试这个代码

于 2013-03-13T07:24:55.130 回答
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;
于 2013-03-13T07:51:00.777 回答
1

试试这个

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
    style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
于 2013-03-13T07:20:30.073 回答
1

只需使用 aUISegmentedControl而不是 a UIButton

然后将模式更改为“瞬时”并添加更改操作。

触发操作时,检查selectedSegmentIndex以了解选择了哪个段。

于 2013-03-13T07:21:39.940 回答
1

在其UIView上添加任意数量的按钮。然后使用

UIBarButtonItem *myBarbtn=[[UIBarButtonItem alloc] initWithCustomView:buttonsView];
self.navigationItem.rightBarButtonItem = myBarbtn;
于 2013-03-13T07:23:45.190 回答
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;
于 2013-03-13T07:25:02.667 回答
1

制作一个 s 数组UIBarButtonItem并将其传递给

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

的方法UINavigationItem

于 2013-03-13T07:26:07.440 回答