gpt4 book ai didi

ios - 创建一个能够检查按下的按钮的标题的函数

转载 作者:行者123 更新时间:2023-11-29 02:11:26 25 4
gpt4 key购买 nike

我有一个动态创建按钮的循环。

for (int b = 0; b < _currentPlayerTeams.count; b++)
{
HNTeamObject *team = _currentPlayerTeams[b];
CGFloat buttonY = 168 + ((b + 1) * distanceBetweenButtons) - 23;

NSString *buttonTitle = [NSString stringWithFormat:@"%@ %@",team.teamName, team.seriesName];

UIButton *button = [[UIButton alloc] init];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"images.png"] forState:UIControlStateNormal];
button.titleLabel.textColor = [UIColor blackColor];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
[button addTarget:self
action:@selector(chooseTeamOne:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle: buttonTitle forState:UIControlStateNormal];
button.titleLabel.text = (@"%@", buttonTitle);
button.frame = CGRectMake(labelAndButtonX, buttonY, 268.0, 46.0);
[self.view addSubview:button];
}

我需要创建一个函数作为选择器,用于创建每个按钮并查看按钮的标题。因为我在循环中制作按钮,所以它们是本地的,不会出现在我创建的另一个函数中。任何帮助,将不胜感激。我提前道歉,因为我是编码的新手,不太了解正在发生的事情。谢谢。

最佳答案

每个按钮在按下时都会调用 chooseTeamOne: 函数,因此我假设您想在该方法中获取按钮标题。

为此,请使用 UIButton 的 title 属性:

 NSLog(@"%@", button.currentTitle);

这将记录按钮的当前标题。需要注意的是,我引用的是“button”,它是调用 chooseTeamOne 方法的 UIButton 实例。我假设 chooseTeamOne 采用 UIButton 按钮作为参数。

如果您的方法采用“id”作为参数,您需要将发送的对象强制转换为 UIButton,如下所示:

- (IBAction)chooseTeamOne:(id)sender {
UIButton *button = (UIButton *)sender;
NSString *buttonTitle = button.currentTitle;
}

关于ios - 创建一个能够检查按下的按钮的标题的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29244073/

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