【问题标题】:Setting UIBarButtonItem not changing title, changing only style设置 UIBarButtonItem 不改变标题,只改变样式
【发布时间】:2017-02-09 14:30:11
【问题描述】:

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

 // 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;

编辑 2:

以下代码正在执行我想要的操作,但是当我第一次单击编辑按钮时,没有任何反应。

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

【问题讨论】:

    标签: ios iphone objective-c uibarbuttonitem


    【解决方案1】:

    这个呢:

    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    

    我认为它几乎可以满足您的需求。

    点击时会变为“完成”,再次点击时会变回“编辑”。此外,它会相应地设置视图控制器的编辑属性。

    【讨论】:

    • 是的,这个功能是内置的。
    • 这是做你想做的正确方法,你可以把它标记为正确答案,其他都是hack。
    • var saveButton = UIBarButtonItem(barButtonSystemItem:UIBarButtonSystemItem.Save, target:self, action:"savePressed:") self.navigationItem.rightBarButtonItem = saveButton
    【解决方案2】:

    就这样做

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

    只需根据点击改变状态。简单的事情。

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

    【讨论】:

    • 到目前为止,这是我想做的(我稍微修改了一下)。但是,在单击时,没有任何反应。我修改后的代码在原帖中。
    【解决方案3】:

    你可以通过代码实现,也可以如下

    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(doneClicked:)];
    self.navigationItem.rightBarButtonItem = rightBarButton;
    

    【讨论】:

    • 这会初始化它。 OP 想要更改标题它已经被创建和显示之后。
    【解决方案4】:

    最简单的解决方案:只需将 BarButtonItem 的标识符更改为自定义即可。

    【讨论】:

    • 不幸的是,这并没有改变对 Title 属性的自定义需求。
    • 这对我有帮助。我总是忘记,如果我在 IB 中将类型设置为自定义以外的任何内容,它将忽略我设置标题的尝试。
    【解决方案5】:

    将类型从 System:Edit 更改为 Custom

    【讨论】:

      【解决方案6】:

      来自UIBarButtonItem reference上的“title”属性

      您应该在将项目添加到栏之前设置此属性。这 默认值为 nil。

      查看this qn also

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-31
        • 1970-01-01
        • 2015-12-16
        • 1970-01-01
        相关资源
        最近更新 更多