Ios 如何设置/获取UISwitch的标记值(NSInteger)
我有使用for循环创建的动态Ios 如何设置/获取UISwitch的标记值(NSInteger),ios,objective-c,Ios,Objective C,我有使用for循环创建的动态ui开关。我为每个控件指定标记值,使其具有标识。我想达到这些价值观以后,但我每次都得到0。我做错了什么 for(int i = 0;i < self.extraArray.count; i++) { ProductPropertiesModel *model = (ProductPropertiesModel *)[self.extraArray objectAtIndex:i]; UISwitch *switchButton = [[U
ui开关
。我为每个控件指定标记值,使其具有标识。我想达到这些价值观以后,但我每次都得到0。我做错了什么
for(int i = 0;i < self.extraArray.count; i++) {
ProductPropertiesModel *model = (ProductPropertiesModel *)[self.extraArray objectAtIndex:i];
UISwitch *switchButton = [[UISwitch alloc] initWithFrame:CGRectZero];
switchButton.translatesAutoresizingMaskIntoConstraints = false;
switchButton.tag = [model Id];
[switchButton setOn:NO];
[self.view addSubview:switchButton];
[switchButton addTarget:self action:@selector(setState:)
forControlEvents:UIControlEventValueChanged];
}
-(void)setState:(id)sender
{
UISwitch *uiswitch = (UISwitch *)sender;
NSInteger tagInteger= uiswitch.tag;
NSLog(@"%@", [NSString stringWithFormat:@"%li",(long)tagInteger]);
}
for(int i=0;i
标记值为0,但这是错误的。请尝试以下操作:
for (int i =0; i < self.extraArray.count; 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(setState:) forControlEvents: UIControlEventValueChanged];
[switchControl setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:switchControl];
y= y+50.0;
}
- (IBAction) setState: (UISwitch *) sender {
NSLog(@"no.%d %@",sender.tag, sender.on ? @"On" : @"Off");
//use sender.tag , you know which switch you selected
}
for(int i=0;i
我建议将[model id]
设置为标记,而应将数组索引设置为标记
然后在稍后的开关控制方法中,您可以使用代码从数组索引中获取值
ProductPropertiesModel *model = (ProductPropertiesModel *)[self.extraArray objectAtIndex:i];
希望这有帮助。我发现了问题。我只是打了个错字。我注意到,在获取json数据时,我正在编写一个拼写错误。我注意到我写的是身份证而不是身份证
NSError *jsonError;
self.extraJsonArray = nil;
self.extraJsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&jsonError];
self.extraArray = [[NSMutableArray alloc]init];
for(int j = 0;j < self.extraJsonArray.count;j++){
@try {
//NSInteger *Id = (NSInteger *)[(NSNumber *)[[self.extraJsonArray objectAtIndex:j]objectForKey:@"Id"] integerValue]; Problem is here
NSInteger *Id = (NSInteger *)[(NSNumber *)[[self.extraJsonArray objectAtIndex:j]objectForKey:@"id"] integerValue];
double price = (double)[(NSNumber *)[[self.extraJsonArray objectAtIndex:j] objectForKey:@"price"] doubleValue];
NSString *property =(NSString *)[[self.extraJsonArray objectAtIndex:j] objectForKey:@"property"];
ProductPropertiesModel *model = [[ProductPropertiesModel alloc] init];
[model setId:Id];
[model setProperty:property];
[model setPrice:price];
[model setChecked:@"No"];
[self.extraArray addObject:model];
} @catch (NSException *exception) {
NSLog(@"%@", exception.reason);
} @finally {
}
}
n错误*jsonError;
self.extraJsonArray=nil;
self.extraJsonArray=[NSJSONSerialization JSONObjectWithData:jsonData选项:NSJSONReadingMutableContainers错误:&jsonError];
self.extraray=[[NSMutableArray alloc]init];
for(int j=0;j
我会设置两个断点。一个用于验证标记设置是否正确(例如,[model Id]
可能存在问题),另一个用于验证发件人是否符合我的预期。作为旁注,-(void)setState:(UISwitch*)sender
可能是您案例中更合适的签名,以避免后续的强制转换,前提是它只能与UISwitch
es一起使用。谢谢阿拉丁,我发现了这个问题。我只是打了个错字。我注意到,在获取json数据时,我正在编写一个拼写错误。我注意到Id正在写入,而不是Id.NSInteger*Id=(NSInteger*)[(NSNumber*)[[self.extraJsonArray objectAtIndex:j]objectForKey:@“Id”]integerValue];谢谢你,苏尔比,我找到问题了。我只是打了个错字。我注意到,在获取json数据时,我正在编写一个拼写错误。我注意到Id正在写入,而不是Id.NSInteger*Id=(NSInteger*)[(NSNumber*)[[self.extraJsonArray objectAtIndex:j]objectForKey:@“Id”]integerValue];