在导航栏中添加栏按钮项

时间:2011-05-25 05:42:43

标签: iphone objective-c xcode

我正在使用导航栏按钮items.i正在使用以下代码来执行此操作

UIBarButtonItem *btnSave = [[UIBarButtonItem alloc] 
                                    initWithTitle:@"Save"                                            
                                    style:UIBarButtonItemStyleBordered 
                                    target:self 
                                 action:@selector(save_Clicked:)];
     self.navigationItem.rightBarButtonItem = btnSave;
     [btnSave release];

     UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] 
                                    initWithTitle:@"Cancel"                                            
                                    style:UIBarButtonItemStyleBordered 
                                    target:self 
                                    action:@selector(save_Clicked)];
     self.navigationItem.leftBarButtonItem = btnCancel;
     [btnCancel release];

我的问题是如何在左侧栏按钮项旁边添加另一个按钮。 提前谢谢

4 个答案:

答案 0 :(得分:8)

要执行此操作,您需要创建一个工具栏,然后继续向其添加UIButton,然后将工具栏设置为leftBarButton

类似的东西:

UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 250, 44)];
tools.tintColor = [UIColor clearColor];
[tools setTranslucent:YES];

NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:9];

UIImage *myImage = [UIImage imageNamed:@"AL_HomeMod_Icon.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);

[myButton addTarget:self action:@selector(clickViewHomeMod) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *bi = [[UIBarButtonItem alloc]
                       initWithCustomView:myButton];

[buttons addObject:bi];
[bi release];

myImage = [UIImage imageNamed:@"AL_History_Icon.png"];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);

[myButton addTarget:self action:@selector(clickViewHistory) forControlEvents:UIControlEventTouchUpInside];

bi = [[UIBarButtonItem alloc]
      initWithCustomView:myButton];

[buttons addObject:bi];
[bi release];

myImage = [UIImage imageNamed:@"AL_RX_Icon.png"];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);

[myButton addTarget:self action:@selector(clickViewCustomPopView2) forControlEvents:UIControlEventTouchUpInside];

bi = [[UIBarButtonItem alloc]
      initWithCustomView:myButton];

[buttons addObject:bi];
[bi release];

myImage = [UIImage imageNamed:@"AL_User_Icon.png"];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);

[myButton addTarget:self action:@selector(clickViewCustomPopView:) forControlEvents:UIControlEventTouchUpInside];
bi = [[UIBarButtonItem alloc]
      initWithCustomView:myButton];
[buttons addObject:bi];
popButton = myButton;
[bi release];


// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];

[buttons release];

// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
希望得到帮助

Pondd

答案 1 :(得分:7)

创建一个按钮

UIBarButtonItem *logoutButton = [[UIBarButtonItem alloc] 
                                initWithImage:[UIImage imageNamed:@"logout.png"] 
                                style:UIBarButtonItemStylePlain 
                                target:self action:@selector(doLogout)];

将此按钮添加到导航栏右侧

self.navigationItem.rightBarButtonItem = logoutButton;

或将此按钮添加到导航栏的左侧

self.navigationItem.leftBarButtonItem = logoutButton;

doLogout是一个将在触摸退出按钮

上调用的函数

答案 2 :(得分:2)

我使用以下代码完成了我的任务:

UIToolbar *tools=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 150, 44)];

tools.backgroundColor=[UIColor clearColor];

 [tools setTranslucent:YES];

UIBarButtonItem *optionBtn=[[UIBarButtonItem alloc]initWithTitle:@"Options" style:UIBarButtonItemStyleBordered target:self action:@selector(optionPressed:)];

UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(donePressed:)];

 NSArray *buttons=[NSArray arrayWithObjects:optionBtn,doneBtn, nil];

    [tools setItems:buttons animated:NO];

 self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:tools];

注意:从IOS 5.0起,Apple已经做得更轻松了。它可以作为

完成
self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:optionBtn,doneBtn, nil];

答案 3 :(得分:1)

使用两个按钮创建自定义视图,并使用UIBarButtonItem的{​​{3}}初始值设定项。应该这样做。