设置UIBarButtonItem不更改标题,仅更改样式 [英] Setting UIBarButtonItem not changing title, changing only style

查看:87
本文介绍了设置UIBarButtonItem不更改标题,仅更改样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在右上角有一个 UIBarButtonItem ,上面写着编辑".单击时,将调用此代码-我知道是因为按钮被加粗了.但是,按钮的标题不会更改.这是我的代码:

I have a UIBarButtonItem in the top right which says 'Edit'. When clicked, this code is called - I know so because the button is emboldened. However, the button's title does not change. Here is my code:

 // set to not editing and change buttons
        [self setEditing:YES animated:YES];
        UIBarButtonItem *doneButton = (UIBarButtonItem *)sender;
        [doneButton setTitle:@"Done"];
        [doneButton setStyle:UIBarButtonItemStyleDone];
        self.navigationItem.rightBarButtonItem = doneButton;

下面的代码正在执行我想要的操作,但是,当我第一次单击编辑"按钮时,什么也没发生.

The following code is doing what I want, however the first time I click the edit button, nothing happens.

-(void)editButton:(id)sender {
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style: UIBarButtonItemStylePlain target:self action:@selector(edit:)];

}

-(void) edit:(UIBarButtonItem *) barBtnItem
{
    // if not editing
    if (![self isEditing])
    {
        [self setEditing:YES];
        barBtnItem.tag = 1;
        [self.navigationItem.rightBarButtonItem setTitle:@"Done"];
    }
    else
    {
        [self setEditing:NO];
        barBtnItem.tag = 0;
        [self.navigationItem.rightBarButtonItem setTitle:@"Edit"];
    }
}

推荐答案

只需执行

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style: UIBarButtonItemStyleBordered target:self action:@selector(edit:)];
barButton.tag = 0;
barButton.possibleTitles = [NSSet setWithObjects:@"Edit", @"Done", nil];

只需单击一下即可更改状态.简单的事情.

Just change state according to click. simple thing.

-(void) edit:(UIBarButtonItem *) barBtnItem
{
     if (barBtnItem.tag == 0)
    {
       barBtnItem.tag = 1;
       [self.navigationItem.rightBarButtonItem setTitle:@"Done"];
    }
    else
    {
        barBtnItem.tag = 0;
        [self.navigationItem.rightBarButtonItem setTitle:@"Edit"];
    } 
}

这篇关于设置UIBarButtonItem不更改标题,仅更改样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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