如何在运行时更改 UINaviagationBar 中 UIBarButtonItem 的类型?
我正在研究 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一个具有此行为的内置栏按钮,您可以通过 UIViewContoller 的
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.如果您通过 IB 添加了按钮,请确保将标识符设置为自定义
还可以在 .h 中使用适当的 IBOutlet 和属性分配一个按钮
在 .m 中合成按钮
然后在代码中执行以下操作:
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:
要更改按钮“完成”按钮,请使用此按钮
将按钮更改为“编辑”按钮,请使用此按钮
to change the button the Done button use this
to change the button to Edit button use this
我最终做了这样的事情。不幸的是,直接设置标题不起作用,由于某种原因它是零并且不允许我将其设置为不同的值。 self.editButton 来自具有目标和操作集的 IBOutlet。这段代码使用了ARC。我希望这对某人有帮助。
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.