将 UIBarButtonSystemItemAdd 和 UIBarButtonSystemItemTrash 添加到导航项或 UISegmentedControl 的一侧

发布于 2024-08-16 21:19:39 字数 201 浏览 4 评论 0原文

我想要一个 UIViewController ,它在标题右侧有一个“添加”和一个“垃圾箱”按钮,可以通过向导航项/栏添加两个 UIBarButtonItems 或添加将它们添加到 UISegmentedControl 中,然后将 SegmentedControl 添加到项目中。这可能吗?如果是,如何实现最好?

I'd like to have a UIViewController which has an Add and a Trash button both right to the title, either by adding two UIBarButtonItems to the navigation item/bar or by adding them to a UISegmentedControl and then adding the SegmentedControl to the item. Is this possible? If yes, how is it achieved best?

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

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

发布评论

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

评论(2

謸气贵蔟 2024-08-23 21:19:39

您可以通过将多个按钮包装在 UIToolbar 中来向导航项添加多个按钮,示例代码< /a>.

You can add multiple buttons to a navigation item by wrapping them in a UIToolbar, sample code.

不知所踪 2024-08-23 21:19:39

我也做过类似的事情。我通过创建一个包含两个 UIButton 的 UIView 子类,将两个 UIButton 添加到导航项/栏。然后,您可以执行以下操作:

MyUIViewSubclass *tempview = [[[MyUIViewSubclass alloc] initWithFrame:CGRectMake(234,4,84,30)] autorelease];
UIBarButtonItem newButton = [[[UIBarButtonItem alloc] initWithCustomView:tempview] autorelease];
[self.navigationItem setRightBarButtonItem:newButton animated:NO];

您所要做的就是在 MyUIViewSubclass 中布局按钮,就可以了。

此外,我还在自定义 init 命令中传递目标 ID,以便更轻松地定位视图中的按钮。因此,对于 MyUIViewSubclass 而不是 initWithFrame,我有这样的东西:

- (id)initWithFrame:(CGRect)aRect andTarget:(id)newTarget {
    if (self = [super initWithFrame:aRect]) {



        UIButton *editbtn = [[[UIButton alloc]  initWithFrame:fframe] autorelease];
        [editbtn addTarget:newTarget action:@selector(MBEdit) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:editbtn];
        [self setFirstbutton:editbtn];
        [editbtn release];



        UIButton *newbtn = [[[UIButton alloc]  initWithFrame:fframe] autorelease];
        [newbtn addTarget:newTarget action:@selector(MBNew) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:newbtn];
        [self setSecondbutton:newbtn];
        [newbtn release];

    }

    return self;

}

I have done something similar. I've added two UIButtons to a navigation item/bar by creating a UIView subclass that has two UIButtons in it. You can then do something like this:

MyUIViewSubclass *tempview = [[[MyUIViewSubclass alloc] initWithFrame:CGRectMake(234,4,84,30)] autorelease];
UIBarButtonItem newButton = [[[UIBarButtonItem alloc] initWithCustomView:tempview] autorelease];
[self.navigationItem setRightBarButtonItem:newButton animated:NO];

All you have to do is layout the buttons in MyUIViewSubclass and you're good.

Also, I pass the ID of the target along in a customized init command to make for easier targeting of the buttons in the view. So for MyUIViewSubclass instead of initWithFrame, I have something like this:

- (id)initWithFrame:(CGRect)aRect andTarget:(id)newTarget {
    if (self = [super initWithFrame:aRect]) {



        UIButton *editbtn = [[[UIButton alloc]  initWithFrame:fframe] autorelease];
        [editbtn addTarget:newTarget action:@selector(MBEdit) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:editbtn];
        [self setFirstbutton:editbtn];
        [editbtn release];



        UIButton *newbtn = [[[UIButton alloc]  initWithFrame:fframe] autorelease];
        [newbtn addTarget:newTarget action:@selector(MBNew) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:newbtn];
        [self setSecondbutton:newbtn];
        [newbtn release];

    }

    return self;

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