如何在iPhone中动态更改按钮标题

时间:2012-04-13 07:43:08

标签: iphone

我有一个要求就是需要用现有标题更改按钮标题 例如按钮标题是“其他”,在运行时需要更改“其他(3)”。需要知道如何使用按钮标题拟合整数值(3)。

请帮助解决这个问题 这是按钮源代码。

 mOthersBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [mOthersBtn setTitle:@"Others" forState:UIControlStateNormal];
        [mOthersBtn addTarget:self action:@selector(mOthersClick) forControlEvents:UIControlEventTouchUpInside];
        mOthersBtn.frame = CGRectMake(20, 320, 280, 25);
        [self.view addSubview:mOthersBtn];

提前致谢..

3 个答案:

答案 0 :(得分:3)

只需使用stringWithFormat:

NSInteger count = 3;
[button setTitle:[NSString stringWithFormat:@"Others (%i)", count] forState:UIControlStateNormal];

答案 1 :(得分:-1)

尝试以下代码

NSString *str = [NSString stringWithFormat:@"Run time text here"];


[button setTitle: str forState:UIControlStateNormal];

答案 2 :(得分:-1)

NSInteger c = 2; [button setTitle:[NSString stringWithFormat:@“Others(%i)”,c] forState:UIControlStateNormal];