切换编辑/完成时分配/释放新的 UIBarButtonItems 更好吗?为啥?
Posted
技术标签:
【中文标题】切换编辑/完成时分配/释放新的 UIBarButtonItems 更好吗?为啥?【英文标题】:Is it better to alloc/dealloc new UIBarButtonItems when toggling Edit/Done? Why?切换编辑/完成时分配/释放新的 UIBarButtonItems 更好吗?为什么? 【发布时间】:2010-03-16 05:40:50 【问题描述】:Apple 的文档暗示,对于可使用“编辑/完成”按钮进行编辑的 UITableView,您应该在每次切换按钮时创建和销毁该按钮。
这是一个执行此操作的代码“BonjourWeb”示例代码项目的 sn-p:
if (editing)
// Add the "done" button to the navigation bar
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneAction:)];
self.navigationItem.leftBarButtonItem = doneButton;
[doneButton release];
[self addAddButton:YES];
else
if ([self.customs count])
// Add the "edit" button to the navigation bar
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editAction:)];
self.navigationItem.leftBarButtonItem = editButton;
[editButton release];
这真的比仅仅编辑按钮的标题更好吗?是否有一些我没有看到的性能优化?或者这只是糟糕的示例来源?
【问题讨论】:
+1 用于使用代码块。 SO上为数不多的真正知道如何使用该站点的新用户之一。 :) 【参考方案1】:我不知道他们为什么在该代码示例中这样做,但有一种更简单的方法可以为任何类型的视图控制器添加编辑/完成按钮(自 SDK 2.0 起可用)。 UIViewController 带有自己的编辑按钮项,因此您可以这样做:
- (void)viewDidLoad
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
编辑按钮将负责将视图控制器转换为编辑模式和退出编辑模式,并相应地更新按钮样式。
【讨论】:
【参考方案2】:Apple 提供了多个 默认 UIBarButtonItem
s。使用这些名为UIBarButtonSystemItem 的默认按钮将使用户能够识别此按钮在每个使用这些按钮的应用程序中执行的操作。 Apple 要求根据其 HIG 使用这些默认按钮。
所以答案归结为:
更改按钮的标题与使用默认的“完成”和“编辑”按钮不同。这些具有不同的外观(例如,“完成”按钮使用浅蓝色)。
【讨论】:
以上是关于切换编辑/完成时分配/释放新的 UIBarButtonItems 更好吗?为啥?的主要内容,如果未能解决你的问题,请参考以下文章