以编程方式添加 UIBarButtonItem 或编辑通过 IB 插入的 UIBarButtonItem 时出现问题

发布于 2024-10-21 17:06:22 字数 878 浏览 5 评论 0原文

我正在开发一个项目,该项目使用底部的选项卡栏来翻转我的应用程序的 5 个部分。其中一个部分会加载一个地图视图,然后生成一个 Web 视图。起初这是使用圆形矩形按钮完成的,但它们妨碍了视图,所以我决定切换到导航栏。在 Interface Builder 中,我插入了一个导航项,并在其中插入了两个栏按钮项(左侧和右侧)。我已经获得了两个按钮的功能,通过从文件所有者控制拖动来触发与每个按钮关联的方法。这工作正常并启动了我的网络视图,但我希望能够做但到目前为止尚未弄清楚如何操作,是将其中一个按钮更改为网络中的“完成”或“后退”按钮看法。我尝试以编程方式创建两个按钮,但都不会出现。这是我尝试用来完成此操作的代码...

UIBarButtonItem *updatePosition = [[UIBarButtonItem alloc] initWithTitle:@"Update Position" 
style:UIBarButtonItemStylePlain target:self action:@selector(findMe)];

self.navigationItem.leftBarButtonItem = updatePosition;

[updatePosition release];

当我运行应用程序时,该按钮不会显示在我使用 Xcode 插入的导航项上。我可以让按钮出现的唯一方法是使用 Interface Builder 插入它们,正如我之前提到的。但是,一旦我打开新视图,我就无法将左侧按钮更改为“后退”或“完成”按钮。我尝试运行此代码来更改使用 IB 插入此代码的 UIBarButtonItem 上按钮的样式

self.navigationItem.leftBarButtonItem.style = UIBarButtonItemStyleDone;

,但无济于事。

I'm working on project that uses a tab bar at the bottom to flip through 5 sections of my app. One of these sections loads a map view which then spawns a web view. This was done with Round Rect Buttons at first, but they got in the way of the views, so I decided to switch to a navigation bar. In Interface Builder I inserted a navigation item and within it two Bar Button Items (left and right side). I've gotten the functionality for both button to work by control dragging from file's owner to fire the method associated with each button. This works fine and launches my web view, but what I would like to be able to do and have yet to figure out how to so far, is changing one of the buttons to a "done" or "back" button once in the web view. I've tried creating both buttons programmatically but neither would appear. This is the code I tried using to accomplish this...

UIBarButtonItem *updatePosition = [[UIBarButtonItem alloc] initWithTitle:@"Update Position" 
style:UIBarButtonItemStylePlain target:self action:@selector(findMe)];

self.navigationItem.leftBarButtonItem = updatePosition;

[updatePosition release];

When I run the app, the button does not show up on the navigation item I inserted using Xcode. The only way I can get the buttons to appear is by inserting them with Interface Builder as I previously mentioned. But then I can not change the left button to a "back" or "done" button once I've brought up the new view. I tried running this code to change the style of the button on the UIBarButtonItem I inserted using IB with this code

self.navigationItem.leftBarButtonItem.style = UIBarButtonItemStyleDone;

But to no avail.

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

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

发布评论

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

评论(1

往昔成烟 2024-10-28 17:06:22

以下是一些可以为您提供帮助的实用方法:

+ (UIBarButtonItem *)setRightBarButtonItem:(SEL)action target:(id)sender withImage:(NSString *)imageName {
    UIImage *buttonImage = [UIImage imageNamed:imageName];
    UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithImage:buttonImage
                                                                    style:UIBarButtonItemStyleBordered
                                                                   target:sender
                                                                   action:action] autorelease];

    return buttonItem;
}

+ (UIBarButtonItem *)setRightBarButtonItem:(SEL)action target:(id)sender withBarButtonSystemItem:(UIBarButtonSystemItem)systemItem {
    UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem
                                                                                 target:sender
                                                                                 action:action] autorelease];

    return buttonItem;
}

用法:

// Done button.
self.navigationItem.rightBarButtonItem = [Utility setRightBarButtonItem:@selector(doneTapped:) target:self withBarButtonSystemItem:UIBarButtonSystemItemDone];

Here are some Utility methods to help you along the way:

+ (UIBarButtonItem *)setRightBarButtonItem:(SEL)action target:(id)sender withImage:(NSString *)imageName {
    UIImage *buttonImage = [UIImage imageNamed:imageName];
    UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithImage:buttonImage
                                                                    style:UIBarButtonItemStyleBordered
                                                                   target:sender
                                                                   action:action] autorelease];

    return buttonItem;
}

+ (UIBarButtonItem *)setRightBarButtonItem:(SEL)action target:(id)sender withBarButtonSystemItem:(UIBarButtonSystemItem)systemItem {
    UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem
                                                                                 target:sender
                                                                                 action:action] autorelease];

    return buttonItem;
}

Usage:

// Done button.
self.navigationItem.rightBarButtonItem = [Utility setRightBarButtonItem:@selector(doneTapped:) target:self withBarButtonSystemItem:UIBarButtonSystemItemDone];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文