在导航栏中添加栏按钮项目

发布于 2024-11-09 17:12:55 字数 957 浏览 0 评论 0原文

我正在使用导航栏按钮项目。我使用以下代码来执行此操作,

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];

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

i was working with navigation bar button items.i was using the following code to do so

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];

my question is how to add another button just adjacent to the left bar button item.
thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦里人 2024-11-16 17:12:55

为此,您需要创建一个工具栏,然后继续向其中添加 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有帮助

To do this you need to create a toolbar then keep adding UIButton to it, then set the toolbar as the leftBarButton

something like this:

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];

hope that help

Pondd

扮仙女 2024-11-16 17:12:55

创建一个按钮,

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 是一个在触摸注销按钮时调用的函数

Create a button as

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

Add this button to right of navigation bar

self.navigationItem.rightBarButtonItem = logoutButton;

or add this button to left side of navigation bar

self.navigationItem.leftBarButtonItem = logoutButton;

doLogout is a function which will be called on touch logout button

み零 2024-11-16 17:12:55

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

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 开始,苹果已经做得更容易了。可以这样做

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

I achieved my task by using the following code :

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];

NOTE:From IOS 5.0 onwards, apple has done it lot easier . It can be done as

self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:optionBtn,doneBtn, nil];
九公里浅绿 2024-11-16 17:12:55

创建一个带有两个按钮的自定义视图,并使用 UIBarButtonIteminitWithCustomView:初始化程序。应该可以做到这一点。

Create a custom view with two buttons and use UIBarButtonItem's initWithCustomView: initializer. That should do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文