gpt4 book ai didi

ios - UIButton 使用计数器在按下时更改标题

转载 作者:行者123 更新时间:2023-11-28 22:26:26 24 4
gpt4 key购买 nike

我在下面的代码中遇到错误。我正在用头撞墙,因为我不明白为什么我似乎无法使用按钮按下次数(计数器)更新消息每次按下按钮...

- (IBAction)countButtonTapped:(id)sender {

self.pressCount+=1;
//int count = (int)self.pressCount;

//[self.tapCountButton setTitle:@"I've been tapped %i time(s)!" forState:UIControlStateNormal];
//[self.tapCountButton setTitle:@"I've hit the main button!" forState:UIControlStateNormal];
//NSLog(@"I've been tapped %lu time(s)!", (unsigned long)self.pressCount);
[(UIButton *)sender setTitle:@"I've been tapped %@ time(s)!",self.pressCount forState:UIControlStateNormal];

最佳答案

听起来您只需要一个字符串格式化程序。

试试这个:

- (IBAction)countButtonTapped:(id)sender {
UIButton * button = (UIButton *) sender;
self.pressCount+=1;
NSString * titleForButton = [NSString stringWithFormat: @"I've been tapped %d time(s)!",self.pressCount]
[button setTitle: titleForButton forState: UIControlStateNormal];
}

这里的另一个区别是“self.pressCount”可能是一个 NSInteger,所以你想使用“%d”而不是“%@”作为格式参数",表示 NSString 对象。

关于ios - UIButton 使用计数器在按下时更改标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18819516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com