2011-10-15 2 views
7

상황에 따라 달라지는 옵션이있는 작업 시트가 있습니다. 버튼 제목의 배열을 먼저 만들고 싶으면 다른 버튼 제목이 충분하지만,이를 varargs 형식으로 변환하는 방법을 알 수는 없습니다.UIActionSheet varargs init 메소드에 문자열 배열을 보내려면 어떻게해야합니까?

나는 이런 식으로 뭔가를 할 :

NSMutableArray *buttonTitles = [NSMutableArray array]; 
if (condition1) { 
    [buttonTitles addObject: @"Do action 1"]; 
} 
if (condition2) { 
    [buttonTitles addObject: @"Do action 2"]; 
} 
if (condition3) { 
    [buttonTitles addObject: @"Do action 3"]; 
} 
if (condition4) { 
    [buttonTitles addObject: @"Do action 4"]; 
} 
UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: @"Cancel" destructiveButtonTitle: nil otherButtonTitles: buttonTitles] autorelease]; 

를 이제 분명히 내가 대신이 같은 뭔가를 할 수에 내가 가진 경우 :

UIActionSheet *actionSheet = nil; 
if (condition1 && condition2 && condition3 && condition4) { 
    actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: @"Cancel" destructiveButtonTitle: nil otherButtonTitles: @"Do action1", @"Do action2", @"Do action3", @"Do action 4", nil] autorelease];  
} else if (condition1 && condition2 && condition3 && !condition4) { 
    actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: @"Cancel" destructiveButtonTitle: nil otherButtonTitles: @"Do action1", @"Do action2", @"Do action3", nil] autorelease];  
} 
// else ... 
// about 14 other cases! 

을하지만 그건 끔찍한 일 것이다. 누구든지 나를 도울 수있는 훌륭한 통사론을 알고 있습니까?

편집 : 내가 그것의 얼굴에 큰 보이는 addButtonWithTitle를 사용하는 것이 제안되었다 는, 불행히도 이것은 바람직하지 않다 취소 버튼, 후 추가 버튼을 넣습니다. (가) 다른 모든 옵션 아래에서 취소 버튼을 선호

// adds a button with the title. returns the index (0 based) of where it was added. buttons are displayed in the order added except for the 
// destructive and cancel button which will be positioned based on HI requirements. buttons cannot be customized. 

HI 요구 사항 (애플의 휴먼 인터페이스 가이드 라인), 그래서 나는 애플을 말하고 싶지만 :

나는이 addButtonWithTitle 상태에서 자신의 문서부터 애플의 코드에 버그 믿는다 망했다. 물론, 그게 정말 도움이되지 않습니다. 그래서 NSArray와 varargs 사이를 변환하려고합니다. 아직 어떻게해야할지 모르겠군요.

for (Nstring *button in buttonTitles) 
    [actionSheet addButtonWithTitle:button]; 

답변

18

이 같은이

NSMutableArray *buttonTitles = [NSMutableArray array]; 
if (condition1) { 
    [buttonTitles addObject: @"Do action 1"]; 
} 
if (condition2) { 
    [buttonTitles addObject: @"Do action 2"]; 
} 
if (condition3) { 
    [buttonTitles addObject: @"Do action 3"]; 
} 
if (condition4) { 
    [buttonTitles addObject: @"Do action 4"]; 
} 
[buttonTitles addObject: @"Cancel"]; 
UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: nil destructiveButtonTitle: nil otherButtonTitles: nil] autorelease]; 

for (NSString *title in buttonTitles) { 
    [actionSheet addButtonWithTitle: title]; 
} 

[actionSheet setCancelButtonIndex: [buttonTitles count] - 1]; 
+0

나는이 방법이 효과가 있다고 생각했지만 편집과 관련하여 몇 가지 문제를 발견했습니다. –

+0

수정을 확인하십시오. –

+0

이것은 효과가 있습니다. 그래도 추가 타이틀을 망쳐 놓을 필요는없는 것처럼 보였다. –

4

왜 그냥하지 뭔가를 시도 할 수 있습니다 buttonIndex 대신 버튼 제목과 비교 :

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex { 
    if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:kSMSButtonText]) { 
    //share via sms 
    }else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:kEmailButtonText]) { 
    //share via mail 
    } 
} 

사람들이 놓칠 수없는 핵심 사항은 다른 모든 버튼 다음에 추가하고 setCancelButtonIndex :를 호출하여 cancel 버튼 모양을 지정하는 대신 initWithTitle 선택기에서 cancelButton을 설정하는 것입니다.

+0

나는 이것이 효과가 있다고 생각했지만, 문제를 발견했다. 편집해라. –

5

Himanshu의 대답에 변화 :

UIActionSheet * as = [[UIActionSheet alloc] initWithTitle:@"Share" 
               delegate:self 
             cancelButtonTitle:nil /* don't set Cancel title here! */ 
            destructiveButtonTitle:nil 
             otherButtonTitles:nil]; 
int cancelButtonIndex = 0; // will remain 0 if no other buttons besides "Cancel" 
if (canShareBySMS) 
{ 
    [as addButtonWithTitle:kSMSButtonText]; 
    cancelButtonIndex++; 
} 
if (canShareByMail) 
{ 
    [as addButtonWithTitle:kEmailButtonText]; 
    cancelButtonIndex++; 
} 
/* etc. */ 

// after all other buttons have been added, include Cancel 
[as addButtonWithTitle:@"Cancel"]; 
[as setCancelButtonIndex:cancelButtonIndex]; 

[as showInView:self.sharingViewController.view]; 
[as release]; 

참고 : UIActionSheetDelegate 방법을 확인해야

0

여기 제가 시도한 방법이 잘 작동하는 것 같습니다. 이것은 "취소"버튼 전에 버튼 끝에 추가 버튼을 추가하려는 경우에 사용됩니다.

NSString * optionalButton = nil; 
if (condition4) { 
    optionalButton = @"Some Additional Option"; 
} 

UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:@"Some Title" 
                  delegate:self 
               cancelButtonTitle:@"Cancel" 
              destructiveButtonTitle:nil 
               otherButtonTitles:@"Button 1",@"Button 2",@"Button 3",optionalButton, nil]; 


[actionSheet showInView:self.view]; 

(condition4) 당신의 조건이 충족되지 않은 경우 버튼 목록의 마지막에 추가 '전무'을 추가 괜찮을 것 같습니다.

iOS7에서 성공적으로 테스트했습니다.