gpt4 book ai didi

iphone - 如何在iPhone中的for循环中创建UISwitch?

转载 作者:行者123 更新时间:2023-12-03 19:44:21 26 4
gpt4 key购买 nike

我在 Viewdid Load 中完成了以下代码,用于以编程方式创建多个开关。

float x =40.0, y=20.0,height=60.0,width=26.0;

for (int i =0; i < 3; i++) {

CGRect frame = CGRectMake(x, y, height, width);
UISwitch *switchControl = [[UISwitch alloc] initWithFrame:frame];
[switchControl addTarget:self action:@selector(flip:) forControlEvents:UIControlEventTouchUpInside];

[switchControl setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:switchControl];

y= y+50.0;
}

- (IBAction) flip: (id) sender {
UISwitch *onoff = (UISwitch *) sender;
NSLog(@"%@", onoff.on ? @"On" : @"Off");
}

完成此代码后,我可以创建多个 UISwitch。现在我不知道如何处理每个开关上的操作。如果有人知道如何处理所有开关上的操作,请帮助我。我会感激他/她。提前致谢。

最佳答案

for (int i =0; i < 3; i++) {

CGRect frame = CGRectMake(x, y, height, width);
UISwitch *switchControl = [[UISwitch alloc] initWithFrame:frame];

//add tag as index
switchControl.tag = i;
[switchControl addTarget:self action:@selector(flip:) forControlEvents: UIControlEventValueChanged];

[switchControl setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:switchControl];

y= y+50.0;
}

- (IBAction) flip: (id) sender {
UISwitch *onoff = (UISwitch *) sender;
NSLog(@"no.%d %@",onoff.tag, onoff.on ? @"On" : @"Off");
//use onoff.tag , you know which switch you got
}

关于iphone - 如何在iPhone中的for循环中创建UISwitch?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10613768/

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