0
我已經創建了一個複選框複選框附加字符串,但當我選擇 只有一個對象後,我的對象包含一個逗號,我不想逗號如果用戶 選擇只在複選框。如果用戶選擇所有三個逗號分隔值是好的,但當用戶選擇隨機然後我們有一個問題。以下是我的代碼追加字符串對象的複選框選擇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語法更新:
您可以在所有三種情況下添加「,」,然後在完成組合最終字符串時刪除* 1個字符。合理? – dklt
順便說一句 - 不要使用'setValue:forKey:'。使用'setObject:forKey:'。 – rmaddy
我沒有以前的選定值。用戶將選擇三個複選框中的任何一個。在複選框選擇我想要結果這樣的事情。例如,如果選中一個複選框,則結果應爲1,如果用戶選擇2複選框,則結果應爲1,2。但在我的情況1,2 ,.我不想要最後一個逗號。用戶可以選擇三個選項中的任何一個Plz幫助我 – user2168844