How to change the text of a BarButtonItem on the N

2019-04-20 03:52发布

I am trying to create a list of items that can be edited. Something like this:

enter image description here

To that end, I added a NavigationBar to the top of the view, then added 2 Bar Button Items in XCode designer. I set the identifier for the button on the left to Add, for the button on the right to Edit.

When I click the Edit, I want to change the text to Done. I've tried various ways, e.g. btnEdit.Title = "Done", but it simply doesn't take.

I've seen several blog posts recommending .SetTitle, but UIButtonBarItem doesn't have that method (at least in MonoTouch).

So, how can I change the title of the Edit button?

6条回答
相关推荐>>
2楼-- · 2019-04-20 04:39
if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"Edit"]) 
{
    self.navigationItem.rightBarButtonItem.title= @"Done";
}

hope this helps. happy coding :)

查看更多
神经病院院长
3楼-- · 2019-04-20 04:48

I resolved it. The key was I was setting the identifier of the Edit button to a system value (e.g. UIBarButtonSystemItemEdit) and you can't change the text of those (makes sense now). I changed the identifier back to Custom and setting .Title worked fine.

查看更多
贼婆χ
4楼-- · 2019-04-20 04:50

Why don't you try changing the navigationItem.rightbarButtonItem property ?

1. Set up two buttons, one for edit and one for done

 UIBarButtonItem*editButton=[[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(editAction)];

 UIBarButtonItem*doneButton=[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(doneAction)];

2. And wherever necessary, in editAction preferrably change the rightBarButtonItem like:

self.navigationItem.rightBarButtonItem=doneButton;

And if you need the editButton back

 self.navigationItem.rightBarButtonItem=editButton;
查看更多
叛逆
5楼-- · 2019-04-20 04:50

Re-new the button like below . .

self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "title", style: UIBarButtonItemStyle.plain, target: self, action: #selector(actionMethod))
查看更多
够拽才男人
6楼-- · 2019-04-20 04:52

Works for me like this as UIBarButtonItem *btnEdit; is class memember in .h;

btnEdit = [[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(btnEditClicked)];
self.navigationItem.leftBarButtonItem = btnEdit;
//[btnEdit release];

Now selector called would be:

-(void)btnEditClicked
{
  if([btnEdit.title isEqualToString:@"Edit"])
  {
    [btnEdit setTitle:@"Done"];
  }
  else
  {
    [btnEdit setTitle:@"Edit"];
  }
}
查看更多
一夜七次
7楼-- · 2019-04-20 04:55

Control drag from bar button to file (using assistant editor), create outlet (in this case, its called "barButton". Then, add this:

Swift 3.0:

self.barButton.title = "New title"
查看更多
登录 后发表回答