2012-04-03 1 views
2

사이에 취소 버튼을 추가 할 수 있습니다. 두 개의 다른 버튼 사이의 가운데에 취소 버튼이 있어야합니다. cancelButtonIndex를 1로 설정하려고했지만 두 개의 다른 버튼이있는 경우 인덱스 0과 1에 배치합니다. 버튼의 이름을 변경할 수는 있지만 취소 버튼의 어두운 파란색 포맷을 원합니다. .어떻게 (스택됩니다) 세 개의 버튼이있는 UIAlertView를 만들기 위해 노력하고 UIAlertView (아이폰 OS)에서 (스택)이 개 다른 버튼

EDIT : ** 참고 : 3 개의 버튼이 기본적으로 '기타'버튼처럼 보이는 경우에만 올바른 순서로 3 개의 버튼을 얻는 방법을 알고 있습니다. 취소 버튼의 취소 버튼을 진한 파란색 배경으로 지정하여 일반 취소 버튼처럼 보이게하고 싶습니다. **

나는 아무 소용

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:button1Title,button2Title,nil] autorelease]; 
alert.cancelButtonIndex = 1; 
[alert show]; 

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil] autorelease]; 
alert.cancelButtonIndex = 1; 
[alert addButtonWithTitle:button1Title]; 
[alert addButtonWithTitle:button2Title]; 
[alert show]; 

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:addButtonWithTitle:button1Title,nil] autorelease]; 
alert.cancelButtonIndex = 1; 
[alert addButtonWithTitle:button2Title]; 
[alert show]; 

을 시도했습니다. 내가하려는 일을 성취 할 수 있습니까?

답변

2

나는이 대답에 두 개의 보조 포인트가 다른 버튼에 추가합니다.

1) 제가 아는 한, 애플은 UIAlertView의 합리적인 수정을위한 응용 프로그램을 거부하지 않은,하지만; 그들은 UIAlertView과 같은 클래스의 뷰 계층 구조가 비공개로 간주되어야한다고 말했습니다.

2)이 질문은 당신이 거기에 도착하는 단계가 아니라 최종 목표에 대한 질문이 더 요구해야하는 이유의 좋은 예입니다. 내가이 질문에 대해 알고있는 유일한 이유는 내 대답 here에 남긴 의견의 결과입니다.

답변 : 때문에 의견의

난 단지 2 버튼이 있습니다 경우에도 당신이 버튼을 적재 한 UIAlertView을 만들 찾고있는 것을 알고있다.

나는이 범주에 속하는 코드에 대해 가장 논리적 인 장소를 찾습니다. 일반적으로 경고보기를 조작하는 데 필요한 코드는 show 호출 주위에 있어야하므로 show 대신 호출하는 범주 메서드를 만들고이 메서드는 차례로 show을 호출합니다.

-(void)showWithButtonsStacked{ 
    static NSString *tempButtonTitle = @"SomeUnlikelyToBeUsedTitle"; 
    BOOL willAddFakeButton = (self.numberOfButtons == 2); // Button are only side by side when there's 2 
    if (willAddFakeButton){ 
     self.clipsToBounds = YES; 
     [self addButtonWithTitle:tempButtonTitle]; // add temp button so the alertview will stack 
    } 
    BOOL hasCancelButton = (self.cancelButtonIndex != -1); // If there is a cancel button we don't want to cut it off 
    [self show]; 
    if (willAddFakeButton){ 
     UIButton *cancelButton = nil; 
     UIButton *tempButton = nil; 
     for (UIButton *button in self.subviews) { 
      if ([button isKindOfClass:[UIButton class]]){ 
       if (hasCancelButton && [button.titleLabel.text isEqualToString:[self buttonTitleAtIndex:self.cancelButtonIndex]]){ 
        cancelButton = button; 
       } else if ([button.titleLabel.text isEqualToString:tempButtonTitle]) { 
        tempButton = button; 
       } 
      } 
     } 
     if (hasCancelButton){ // move in cancel button 
      cancelButton.frame = tempButton.frame; 
     } 
     [tempButton removeFromSuperview]; 

     // Find lowest button still visable. 
     CGRect lowestButtonFrame = CGRectZero; 
     for (UIButton *button in self.subviews) { 
      if ([button isKindOfClass:[UIButton class]]){ 
       if (button.frame.origin.y > lowestButtonFrame.origin.y){ 
        lowestButtonFrame = button.frame; 
       } 
      } 
     } 

     // determine new height of the alert view based on the lowest button frame 
     CGFloat newHeight = CGRectGetMaxY(lowestButtonFrame) + (lowestButtonFrame.origin.x * 1.5); 
     self.bounds = CGRectMake(0, 0, self.bounds.size.width, newHeight);   
    } 
} 

이 방법은 그것의 목표 달성 방법은 다음 임시 버튼을 제거하고 높이를 조정, 버튼을 스택 경고 뷰를 강제로 경고보기에 임시 버튼을 추가하는 것입니다.이 같은 경고에

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test title" message:@"message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
[alert showWithButtonsStacked]; 

이 코드의 결과를 : 그것은 카테고리 방법을 사용하여 호출하여 간단하게 사용할 이래로이 버튼 순서로까지 일하는 것이

enter image description here

+0

완벽하게 작동합니다 - 감사합니다! – SAHM

+0

어떻게 신속하게 구현할 수 있습니까? – iOS

3
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self  cancelButtonTitle:nil otherButtonTitles:nil] autorelease]; 
[alert addButtonWithTitle:button1Title]; 
[alert addButtonWithTitle:@"Cancel"]; 
[alert addButtonWithTitle:button2Title]; 
[alert show]; 

도움이 될 수 있습니다,

건배.

+0

, 그러나 아니다 진정한 취소 버튼. 다른 두 개의 버튼과 동일한 배경색을 갖습니다. – SAHM

+0

이봐, 만약 당신이 버튼을 동일한 기본 취소 버튼을 원하는 다른 이미지와 자신의 사용자 정의 버튼을 만들고 viewController에 대상을 설정합니다. UIAlertView는 UIView의 하위보기이므로 버튼을 하위보기로 추가 할 수 있습니다. – Nilesh

+0

의견을 보내 주셔서 감사합니다. 그러나 나는이 계급을 재현하려는 것이 아니다. 두 개의 다른 인덱스 사이에 실제 취소 버튼의 인덱스를 가져 오는 방법이 있는지보고 싶었습니다. – SAHM

0

는 설정합니다은 nil에 취소 버튼을 그냥 대신

+0

이것은 버튼 순서까지는 작동하지만 취소 버튼이 아닙니다. 다른 두 개의 버튼과 동일한 배경색을 갖습니다. – SAHM

2
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:nil] autorelease]; 
[alert addButtonWithTitle:button1Title]; 
[alert addButtonWithTitle:@"Cancel"]; 
[alert addButtonWithTitle:button2Title]; 
[alert setCancelButtonIndex:1]; // to make it look like cancel button 
[alert show]; 
+0

그래, 그 중 하나가 작동하지 않습니다. – SAHM

관련 문제