2

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];
    }

这真的比仅仅编辑按钮的标题更好吗?是否有一些我没有看到的性能优化?或者这只是不好的示例来源?

4

2 回答 2

2

我不知道他们为什么在那个代码示例中这样做,但是有一种更简单的方法可以为任何类型的视图控制器添加一个编辑/完成按钮(自 SDK 2.0 起可用)。UIViewController 带有自己的编辑按钮项,因此您可以这样做:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.leftBarButtonItem = self.editButtonItem;
}

编辑按钮将负责将视图控制器转换为编辑模式和退出编辑模式,并相应地更新按钮样式。

于 2010-07-15T18:56:38.537 回答
1

Apple 完成了多个default 。 UIBarButtonItem使用这些称为UIBarButtonSystemItem的默认按钮将使用户能够识别此按钮在使用这些按钮的每个应用程序中执行的操作。Apple 要求根据其 HIG 使用这些默认按钮。
所以答案归结为:更改按钮的标题与使用默认的“完成”和“编辑”按钮不同。这些具有不同的外观(例如,“完成”按钮使用浅蓝色)。

于 2010-03-16T09:37:30.853 回答