繁体   English   中英

如何设置/获取UISwitch的标签值(NSInteger)

[英]How can I set/get tag value (NSInteger) for UISwitch

我有使用for循环创建的动态UISwitches 我给每个控件的标签值赋予一个标识。 我想稍后达到这些值,但是每次都会得到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]);

}

标签值为0,但这是错误的。

我发现了问题。 我刚打错字。 我注意到我在获取json数据时正在写错字。 我注意到我写的是id,而不是id。

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 {

            }
        }

尝试这个:

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 
}

我建议改为将[model id]设置为标签,而应将数组索引设置为标签。

然后在switch控制方法中,您可以使用代码从数组索引中获取值。

  ProductPropertiesModel *model = (ProductPropertiesModel *)[self.extraArray objectAtIndex:i];

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM