切换编辑/完成时分配/释放新的 UIBarButtonItems 是否更好?为什么?

发布于 2024-08-25 05:50:16 字数 956 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

大海や 2024-09-01 05:50:16

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.leftBarButtonItem = self.editButtonItem;
}

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

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:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.leftBarButtonItem = self.editButtonItem;
}

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.

情深如许 2024-09-01 05:50:16

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

There are multiple default UIBarButtonItems 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文