如何在运行时更改 UINaviagationBar 中 UIBarButtonItem 的类型?

发布于 2024-08-08 23:34:18 字数 239 浏览 6 评论 0原文

我正在研究 iPhone 的视图,它由 3 个元素组成:UITextView、UIToolBar 和 UIBarButtonItem。

目标是,我希望 UIBarButtonItem 将其样式从“编辑”(UIBarButtonSystemItemEdit)更改为“完成”(UIBarButtonSystemItemDone)并将新选择器更新为新方法。

首先,我尝试过以下代码,但它不起作用:

你能帮我解决这个问题吗?

I am working on an iPhone's view which composed 3 elements, UITextView, UIToolBar with an UIBarButtonItem.

The goal is, I want UIBarButtonItem change its style from 'edit' (UIBarButtonSystemItemEdit) to 'Done' (UIBarButtonSystemItemDone) and update new selector to new method.

First of all, I have tried following code but it doesn't work:

Could you help me on this idea?

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

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

发布评论

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

评论(4

心清如水 2024-08-15 23:34:18

有一个具有此行为的内置栏按钮,您可以通过 UIViewContoller 的 editButtonItem 属性获取它。点击该按钮会将其来源的视图控制器更改为编辑模式,并将该按钮切换为完成按钮。

- (void)viewDidLoad {
  [super viewDidLoad];
  self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

There is a builtin bar button with this behaviour, you get it via the editButtonItem property of a UIViewContoller. Tabbing that button will change the view controller it came from into editing mode, and toggle the button into a done button.

- (void)viewDidLoad {
  [super viewDidLoad];
  self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
无边思念无边月 2024-08-15 23:34:18

如果您通过 IB 添加了按钮,请确保将标识符设置为自定义
还可以在 .h 中使用适当的 IBOutlet 和属性分配一个按钮
在 .m 中合成按钮

然后在代码中执行以下操作:

// Set to done
editButton.style = UIBarButtonItemStyleDone;
editButton.title = @"Done";

// Set back to edit
editButton.style = UIBarButtonItemStyleBordered;
editButton.title = @"Edit";

If you have added the button through IB then make sure to set the identifier to Custom
Also allocate a button in the .h with appropriate IBOutlet and Property
Synthesize the button in .m

Then in your code do the following:

// Set to done
editButton.style = UIBarButtonItemStyleDone;
editButton.title = @"Done";

// Set back to edit
editButton.style = UIBarButtonItemStyleBordered;
editButton.title = @"Edit";
那片花海 2024-08-15 23:34:18

要更改按钮“完成”按钮,请使用此按钮

[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];

将按钮更改为“编辑”按钮,请使用此按钮

[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleBordered];

to change the button the Done button use this

[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];

to change the button to Edit button use this

[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleBordered];
一人独醉 2024-08-15 23:34:18

我最终做了这样的事情。不幸的是,直接设置标题不起作用,由于某种原因它是零并且不允许我将其设置为不同的值。 self.editButton 来自具有目标和操作集的 IBOutlet。这段代码使用了ARC。我希望这对某人有帮助。

        NSString *title = app.settings.editing 
                        ? NSLocalizedString(@"Done", @"")
                        : NSLocalizedString(@"Edit", @"");

        UIBarButtonItemStyle style  = app.settings.editing 
                                    ? UIBarButtonItemStyleDone
                                    : UIBarButtonItemStyleBordered;

        UIBarButtonItem *editButton 
            = [[UIBarButtonItem alloc] initWithTitle:title 
                                               style:style 
                                              target:self.editButton.target 
                                              action:self.editButton.action];

        self.navigationItem.rightBarButtonItem = editButton;

I ended up doing something like this. Unfortunately, setting the title directly did not work, for some reason it was nil and would not let me set it to a different value. The self.editButton comes from an IBOutlet with the target and actions set. This code uses ARC. I hope this helps someone.

        NSString *title = app.settings.editing 
                        ? NSLocalizedString(@"Done", @"")
                        : NSLocalizedString(@"Edit", @"");

        UIBarButtonItemStyle style  = app.settings.editing 
                                    ? UIBarButtonItemStyleDone
                                    : UIBarButtonItemStyleBordered;

        UIBarButtonItem *editButton 
            = [[UIBarButtonItem alloc] initWithTitle:title 
                                               style:style 
                                              target:self.editButton.target 
                                              action:self.editButton.action];

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