2013-11-25 3 views
0

체크 상자 선택에 확인란을 만들었습니다. 문자열을 추가했지만 객체를 선택하면 그 다음에 쉼표가 포함 된 객체가 쉼표가 필요하지 않습니다. 사용자 확인란 만 선택하십시오. 사용자가 세 개를 모두 선택하면 쉼표로 구분 된 값이 잘되지만 사용자가 무작위로 선택하면 문제가 발생합니다. 아래는 내 코드입니다아이폰에 대한 확인란 선택시 String 객체 추가

- (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]; 
    } 
} 

감사합니다. 이 코드는 정리하고 현대적인 목표 - C 구문으로 업데이트 된 방법

- (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]; 
    } 
} 

참고 : 기존 값이있는 경우

+1

마지막 문자열 작성을 마치면 세 가지 경우 모두에 ","를 추가 한 다음 * 삭제 * 1 문자를 추가 할 수 있습니다. 이해가 되니? – dklt

+0

BTW -'setValue : forKey :'를 사용하지 마십시오. 'setObject : forKey :'를 사용하십시오. – rmaddy

+0

이전에 선택한 값이 없습니다. 사용자가 세 가지 확인란 중 하나를 선택합니다. 확인란을 선택하면 결과가 이와 같이 나타납니다. 예를 들어 하나의 확인란을 선택하면 결과는 1이어야하고 사용자가 2를 선택하면 결과는 1,2입니다. 하지만 제 경우에는 1,2입니다. 나는 마지막 쉼표를 원하지 않는다. 사용자는 세 가지 선택 중 하나를 선택할 수 있습니다. Plz 도와주세요 – user2168844

답변

0

만에 쉼표를 추가합니다.

관련 문제