如何更改UINavigationBar中的UIBarButtonItem [英] How to change a UIBarButtonItem in a UINavigationBar

查看:78
本文介绍了如何更改UINavigationBar中的UIBarButtonItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置可以编辑的项目列表。我有一个主视图,UINavigationBar在顶部和UITableView直接在它下面。我想在点击时将编辑按钮更改为完成按钮,但我无法弄清楚该怎么做。

I'm trying to set up a list of items that can be edited. I have a main view, with a UINavigationBar at the top and a UITableView directly under it. I'd like to have my "edit" button change to a "done" button on click, but I can't figure out how to do it.

如果我可以在代码中(不是界面构建器),我可以替换它,但我甚至不能这样做。我看到一些代码使用[self.navigationItem],但在我的情况下self是一个UIView。

If I could do it in the code (not it interface builder), I could just replace it, but I can't even do that. I've seen some code using [self.navigationItem], but in my case self is a UIView.

当我使用UINavigationBar也感到有点奇怪不想导航(这是一个页面),但我想要一个工具栏标题和一个按钮,所以我不认为真的有一个选择。

It also feels a bit odd to be using a UINavigationBar when I don't want navigation (this is one page only), but I want a toolbar with a title and and a button, so I don't think really have a choice.

推荐答案

我创建了一个按钮,可以从编辑更改为完成。这是更多iPhone开发书的一个提示。

I create one button that can change from Edit to Done. It's a tip from More iPhone Development book.

- (void)viewDidLoad {
    [super viewDidLoad];

    UIBarButtonItem *editButton = self.editButtonItem;  
    [editButton setTarget:self]; 
    [editButton setAction:@selector(toggleEdit)]; 
    self.navigationItem.leftBarButtonItem = editButton; 
}

方法toggleEdit

And the method toggleEdit

- (IBAction)toggleEdit { 
    BOOL editing = !self.tableView.editing; 
    self.navigationItem.rightBarButtonItem.enabled = !editing; 
    if (editing) {
   self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"Done", @"Done");
   //Added in the edition for this button has the same color of the UIBarButtonSystemItemDone
   self.navigationItem.leftBarButtonItem.style = UIBarButtonItemStyleDone;
}
else{
   self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"Edit", @"Edit"); 
   //Added in the edition for this button has the same color of the UIBarButtonSystemItemDone
   self.navigationItem.leftBarButtonItem.style = UIBarButtonItemStylePlain;
}
    [self.tableView setEditing:editing animated:YES]; 
} 

那么你不需要替换任何一个。

Then you don't need replace any of them.

这篇关于如何更改UINavigationBar中的UIBarButtonItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆