为iphone选择复选框时附加字符串对象



我已经在复选框选择上创建了一个复选框,并附加字符串,但当我选择之后只有一个对象我的对象包含逗号,如果用户选中"仅在"复选框。如果用户选择了这三个值,那么逗号分隔的值就可以了,但当用户随机选择时,我们就有问题了。下面是我的代码

- (IBAction)buttonAction:(UIButton *)sender {
    if (sender.tag == 0) {
    if (isPaint) {
        isPaint = NO;
        [self.filterDict setValue:@"" forKey:@"one"];
    } else {
        isPaint = YES;
        [self.filterDict setValue:@"1" forKey:@"one"];
    }
}
if (sender.tag == 1) {
    if (isDecor) {
        isDecor = NO;
        [self.filterDict setValue:@"" forKey:@"two"];
    } else {
        isDecor = YES;
        [self.filterDict setValue:@"2" forKey:@"two"];
    }
}
if (sender.tag == 2) {
    if (isCommunity) {
        isCommunity = NO;
        [self.filterDict setValue:@"" forKey:@"three"];
    } else {
        isCommunity = YES;
        [self.filterDict setValue:@"3" forKey:@"three"];
    }
  }
}
- (IBAction)doneFilter:(UIButton *)sender   {
NSMutableString *filterType = [[NSMutableString alloc] init];
    if ([self.filterDict objectForKey:@"one"] != nil) {
        paintStr = [NSString stringWithFormat:@"%@,", [self.filterDict objectForKey:@"one"]];
    } if ([self.filterDict objectForKey:@"two"] != nil) {
        decorStr = [NSString stringWithFormat:@"%@,", [self.filterDict objectForKey:@"two"]];
    } if ([self.filterDict objectForKey:@"three"] != nil) {
        communityStr = [NSString stringWithFormat:@"%@", [self.filterDict objectForKey:@"three"]];
    }
    if (paintStr != nil) {
        [filterType appendString:paintStr];
    } if (decorStr != nil) {
        [filterType appendString:decorStr];
    } if (communityStr != nil) {
        [filterType appendString:communityStr];
    }
}

提前谢谢。

只有在存在值的情况下才附加逗号:

- (IBAction)doneFilter:(UIButton *)sender   {
    NSMutableString *filterType = [[NSMutableString alloc] init];
    paintStr = self.filterDict[@"one"];
    decorStr = self.filterDict[@"two"];
    communityStr = self.filterDict[@"three"];
    if (paintStr) {
        if (filterType.length) {
            [filterType appendString:@","];
        }
        [filterType appendString:paintStr];
    }
    if (decorStr) {
        if (filterType.length) {
            [filterType appendString:@","];
        }
        [filterType appendString:decorStr];
    }
    if (communityStr) {
        if (filterType.length) {
            [filterType appendString:@","];
        }
        [filterType appendString:communityStr];
    }
}

请注意这些代码是如何用现代Objective-C语法清理和更新的。

最新更新