gpt4 book ai didi

ios - 通过按下 UILabel 打开/关闭 UISwitch

转载 作者:行者123 更新时间:2023-11-29 03:12:27 24 4
gpt4 key购买 nike

我有这个方法,它在它们旁边显示 UILabel 和 UISwitch。我也希望通过按下标签来打开和关闭 UISwitch。我已经标记了开关,但我不知道下一步该做什么。任何帮助将不胜感激。谢谢!

while (i < numberOfAnswers) {
UILabel *answerLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, y+spaceBetweenAnswers, 240, 30)];
answerLabel.text = [NSString stringWithFormat:@"%@ (%@)", questionBank[randomQuestionNumber][1][i][0],questionBank[randomQuestionNumber][1][i][1]];
answerLabel.font = [UIFont systemFontOfSize:15];
answerLabel.textColor = [UIColor darkGrayColor];
answerLabel.numberOfLines=0;
[answerLabel sizeToFit];
[_answerView addSubview:answerLabel];
answerHeight = answerLabel.frame.size.height + spaceBetweenAnswers;

UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(10, y+spaceBetweenAnswers-5, 0, 30)];
mySwitch.transform = CGAffineTransformMakeScale(0.75, 0.75);
if ([questionBank[randomQuestionNumber][1][i][1] isEqual:@"0"]) {
[mySwitch addTarget:self action:@selector(wrongAnswer:) forControlEvents:UIControlEventValueChanged];
} else {
}
mySwitch.tag = i;
[_answerView addSubview:mySwitch];
}

最佳答案

使用 UIButton 而不是 UILabel 并在其上设置 IBAction。您可以将按钮样式设置为看起来与标签完全一样。您的 IBAction 方法应如下所示:

- (void)buttonPressed:(UIButton *)sender
{
//Get the view by tag
UISwitch *mySwitch = (UISwitch *)[self.view viewWithTag:yourTag];
[mySwitch.setOn:![mySwitch isOn]];
}

编辑

如前所述,由于您是在代码中构建 UIButton,因此您需要将目标添加到按钮以获取按钮点击。像这样添加操作:

[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

关于ios - 通过按下 UILabel 打开/关闭 UISwitch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22099614/

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