如何使用 editButtonItem 初始化按钮
我有一个表视图,在该表视图中我正在设置一个节标题,我想在其中包含一个编辑按钮。 我知道如果我的视图控制器是导航链的一部分,那么有一个内置的支持,但是有没有办法在没有它的情况下做到这一点,或者唯一的选择是手动设置一个按钮并触发一个将替换的操作标题和编辑属性? 顺便说一句 - 我知道我下面写的代码是错误的,只是想知道是否有一种方便的方法将 UIBarButtonItem 转换为 UIButton。谢谢
-(UIView*) headerView
{
if (headerView)
return headerView;
float w = [[UIScreen mainScreen] bounds].size.width;
CGRect headerViewFrame = CGRectMake(0,0, w,48);
headerView = [[UIView alloc] initWithFrame:headerViewFrame];
[headerView addSubview:[self editButtonItem]]; // I want to do something like that
}
I have a table view , within that tableview I'm setting up a section header, which I want to contain an edit button.
I know there is a built in support for something like that if my viewcontroller is part of a navigation chain, but is there a way to do it without it, or the only option is to set a button manually and trigger an action that will replace the title and the editing property?
btw - I know the code I wrote below is wrong, just wondering if there is a convenient way to convert a UIBarButtonItem to a UIButton. Thanks
-(UIView*) headerView
{
if (headerView)
return headerView;
float w = [[UIScreen mainScreen] bounds].size.width;
CGRect headerViewFrame = CGRectMake(0,0, w,48);
headerView = [[UIView alloc] initWithFrame:headerViewFrame];
[headerView addSubview:[self editButtonItem]]; // I want to do something like that
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无法将 UIBarButtonItem 转换为 UIButton。如果你查看它们的每个超类,你会发现 UIBarButtonItem 继承自 UIBarItem,而 UIBarItem 继承自 NSObject(与 UIButton 完全无关,UIButton 来自 UIView 并符合 UIResponder 协议)。
对于这个特定问题,您可以重写以下方法:
并使用一个按钮(您可以将其用作节标题视图)触发设置编辑状态的方法
There is no way to convert a UIBarButtonItem into a UIButton. If you look at each of their superclasses you will find that UIBarButtonItem inherits from UIBarItem which inherits from NSObject (not related at all to UIButton which from UIView and conforms to the UIResponder protocol).
For this specific problem you can override the following method:
and have a button (you can use it as your section header view) trigger a method that sets the edit status