如何获取UIBarButtonItem的嵌入按钮
在iPhone应用程序中,我们可以使用以下代码制作一个UIBarButtonItem:
UIBarButtonItem *bbix=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];
生成的UIBarButtonItem具有系统提供的“操作”图标。我的问题是:如何获取此UIBarButtonItem的内部UIButton并将此UIButton添加到另一个UIView?谁可以给我看一些代码?
In an iPhone app,we can make a UIBarButtonItem by using the following code:
UIBarButtonItem *bbix=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];
the generated UIBarButtonItem has a system-provided "action" icon.My question is :how to get this UIBarButtonItem's inner UIButton and add this UIButton to another UIView? who can show me some code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你不能。
UIBarButtonItem
继承自UIBarItem
,而UIBarItem
又继承于NSObject
。这些类都没有获取UIButton
的方法,因为UIBarButtonItem
不是UIButton
。相反,您可以从
UIButton
创建UIBarButtonItem
- 请参阅我对另一个问题的回答以获取更多信息(通过将其添加为“自定义视图”来工作)。You can't.
UIBarButtonItem
inherits fromUIBarItem
which inherits fromNSObject
. None of those classes has a method to get aUIButton
, becauseUIBarButtonItem
isn't aUIButton
.Contrariwise, you can create a
UIBarButtonItem
from aUIButton
-- see an answer I gave to a different question for more info (which works by adding it as a "custom view").访问栏按钮项的“customView”属性是一种可能的解决方案:
尝试一下。它真的对我有用:)
Accessing the "customView" property of the bar button item is one possible solution:
Try this out. It really works for me :)
我认为无论如何你都无法获得嵌入式按钮。但是,如果您使用自定义视图创建按钮,例如使用按钮,我们可以稍后使用以下代码获取该按钮:
自定义视图的层次结构应该像这样:
希望有所帮助。
I don't think you can get the embedded button in any case. However, if you create a button using a custom view, for example, using a button, we can get that button later using this code:
The hierarchy of the custom view should like this:
Hopes that helps.
您无法通过 UIBarButtonItem 访问 UIBarButtonItem 内的嵌入按钮,
您可以使用界面生成器与新的插座链接内部按钮,这样您就可以直接访问该按钮
当您打开 nib 文件时,您可以在对象视图中找到该按钮,然后您可以将其与新的插座链接
You can't access the embedded button inside UIBarButtonItem through the UIBarButtonItem
what you can do it to link the inner button using the interface builder with a new outlet and like this you can access to the button directly
you can find the button inside the objects view when you open the nib file then you can link it with a new outlet
只是为了扩展 Brians 的答案,如果您的 UIBarButton 是使用自定义视图制作的...
只需添加一点错误检查,您永远不知道苹果或其他开发人员有一天会更改布局层次结构。因为从长远来看,这种方法确实很容易出错。
Just to extend Brians answer if your UIBarButton is made with custom view...
Just add a little bit of error checking, you never know when apple or another developer will one day change the layout hierarchy. Since this approach is really error prone long term.
如果您通过 initWithCustomView 创建了栏按钮项目,只需访问自定义视图属性并转换为 UIButton。
http://cocoatouch.blog138.fc2.com/blog-entry-214.html
If you have created your bar button item via initWithCustomView, simply access the custom view property and cast to UIButton.
http://cocoatouch.blog138.fc2.com/blog-entry-214.html
这是一个精确复制
UIBarButtonItem
图像的解决方案,该图像是通过使用UIBarButtonSystemItemAction
系统类型获取的。例如,新创建的 UIButton 被插入到MKAnnotationView
中:创建一个包含此方法的类别文件:
在
MKMapView
委托中,添加此实现(根据需要进行调整) :Here is a solution that precisely replicates the
UIBarButtonItem
image that is otherwise acquired by using theUIBarButtonSystemItemAction
system type. As an example, the newly created UIButton is inserted into anMKAnnotationView
:Create a category file that contains this method:
In the
MKMapView
delegate, add this implementation (adapt as desired):