切换编辑/完成时分配/释放新的 UIBarButtonItems 是否更好?为什么?
Apple 的文档暗示,对于可使用“编辑/完成”按钮进行编辑的 UITableView,您应该在每次切换时创建并销毁该按钮。
下面是执行此操作的“BonjourWeb”示例代码项目的代码片段:
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];
}
这真的比仅编辑按钮标题更好吗?是否有一些我没有看到的性能优化?或者这只是一个糟糕的例子来源?
Apple's documentation implies that for a UITableView editable with an "Edit/Done" button, you should create and destroy the button each time it's toggled.
Here's a snippet of code "BonjourWeb" sample code project that does this:
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];
}
Is this really better than just editing the title of the button? Is there some performance optimisation that I'm not seeing? Or is this just bad example source?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道他们为什么在该代码示例中这样做,但是有一种更简单的方法可以为任何类型的视图控制器添加“编辑/完成”按钮(自 SDK 2.0 起可用)。 UIViewController 带有自己的编辑按钮项,因此您可以这样做:
编辑按钮将负责将视图控制器转换为编辑模式和退出编辑模式,并将相应地更新按钮样式。
I don't know why they do that in that code sample, but there's a much easier way to add an Edit/Done button for any kind of view controller (available since SDK 2.0). UIViewController comes with its own edit button item, so you can just do this:
The edit button will take care of transitioning the view controller into and out of editing mode, and it will update the button style accordingly.
Apple 完成了多个默认
UIBarButtonItem
。使用这些默认按钮,称为 UIBarButtonSystemItem,将使用户能够识别该按钮在每个使用这些按钮的应用程序中执行的操作。 Apple 要求根据其 HIG 使用这些默认按钮。所以答案归结为:
更改按钮的标题与使用默认的“完成”和“编辑”按钮不同。它们具有不同的外观(例如,“完成”按钮使用浅蓝色)。
There are multiple default
UIBarButtonItem
s done by Apple. Using these default buttons, called UIBarButtonSystemItem, will enable the user to identify the action that this button does in every app that uses these. Apple requires to use these default buttons according to their HIGs.So the answer comes down to this:
Changing the title of the button is not the same as using the default "Done" and "Edit" buttons. These have a different look (the "Done" button for example is using a light blue).