1

UIAlertView을 만들었으니 이제 사용자가 누를 버튼을 확인하고 싶습니다.내 UIAlertView의 어떤 버튼을 누르셨습니까?

내 코드는 다음과 같습니다

- (IBAction)button1 { 
    { 
     UIAlertView *alert1 = [[UIAlertView alloc] init]; 
     [alert1 setTitle:@"Hello"]; 
     [alert1 setMessage:@"Do you like smoking?"]; 
     [alert1 addButtonWithTitle:@"Yes"]; 
     [alert1 addButtonWithTitle:@"No"]; 
     [alert1 show]; 
    } 
} 

가 어떻게하면-else 문으로 그것을 확인할 수 있습니까?

+0

대리인 메서드입니다. – Larme

답변

2

, 뷰 컨트롤러에서 UIAlertViewDelegate 프로토콜을 구현합니다 UIAlertView에 대리인으로 설정하고,이처럼 alertView:clickedButtonAtIndex: 이벤트를 기다립니다

@interface MyViewController : UIViewController <UIAlertViewDelegate> 
-(void)alertView:(UIAlertView *)alertView 
     clickedButtonAtIndex:(NSInteger)buttonIndex; 
@end 

@implementation MyViewController 

-(void)alertView:(UIAlertView *)alertView 
    clickedButtonAtIndex:(NSInteger)buttonIndex { 
    NSLog(@"Button %d clicked...", (int)buttonIndex); 
} 

@end 

변경 다음과 같이 경고보기를 표시하는 코드 :

UIAlertView *alert1 = [[UIAlertView alloc] init]; 
[alert1 setTitle:@"Hello"]; 
[alert1 setMessage:@"Do you like smoking?"]; 
[alert1 addButtonWithTitle:@"Yes"]; 
[alert1 addButtonWithTitle:@"No"]; 
alert1.delegate = self; // <<== Add this line 
[alert1 show]; 
0

당신이 할 수있는 가장 좋은 방법은 당신의 경고보기가 UIAlertViewDelegate 프로토콜을 준수하고 방법 –alertView:clickedButtonAtIndex:을 구현 제시 클래스를 만드는 것입니다. 그렇게하면 어떤 버튼이 클릭되었는지 알 수 있습니다.

희망이 도움이됩니다.

4

당신은 자신에게 UIAlertView에서 콜백을 처리 할 클래스에 UIAlertView의 대리자를 설정해야합니다

일예 self이 프로토콜 <UIAlertViewDelegate>을 구현 현재 UIViewController입니다

[alert1 setDelegate:self]; 

.

UIAlertViewUIAlertView을 생성 한 UIViewController을 사용하는 예에서 대리자로 설정 한 개체를 다시 호출합니다. 콜백을 받으면 어떤 버튼 인덱스가 탭되었는지 확인하고 이에 따라 조치 할 수 있습니다. 이것은 iOS 개발, 특히 UIKit 전반에 걸쳐 사용되는 기본적인 위임 패턴입니다.

@interface MyViewController : UIViewController() <UIAlertViewDelegate> 
@end 

@implementation MyViewController 

- (IBAction)button1 { 
    { 
     UIAlertView *alert1 = [[UIAlertView alloc] init]; 
     [alert1 setTitle:@"Hello"]; 
     [alert1 setMessage:@"Do you like smoking?"]; 
     [alert1 addButtonWithTitle:@"Yes"]; 
     [alert1 addButtonWithTitle:@"No"]; 
     [alert1 setDelegate:self]; 
     [alert1 show]; 
    } 
} 

#pragma mark - UIAlertViewDelegate 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    // Handle interaction 
     switch (buttonIndex) 
     { 
      case 0: 
      NSLog(@"Yes was pressed"); 
      break; 
      case 1: 
      NSLog(@"No was pressed"); 
      break; 
     } 
} 

@end 

그것은 매우 중요합니다 당신은 헤더 파일에 지정하거나 클래스는 <UIAlertViewDelegate> 구현 클래스 인터페이스 확장.

자세한 내용은 UIAlertViewDelegate Protocol Reference을 확인하고 다른 많은 UIKit 구성 요소에 대해서는이 방법을 사용할 수 있습니다.

+0

나는 이것이 1 년 된 것을 알고있다. 그러나 내가 발견 한 코드조차도 그것은 오래되지 않는다. iOS 8.1 및 Xcode 6.1 테스트 및 작동! :) 나는 개인적으로 두 가지를 더 추가해야했지만, 여전히 그것을 적용하고 놀랍도록 실행됩니다! 감사!!! –

+0

다행입니다. 아마도 'UIAlertController'로 옮겨가는 것이 좋습니다. – Tim

+0

어제 Apple Docs를 뒤적 거리며 보았습니다. 여전히이 기능을 사용할 수 있습니까? –

1

Jeff가 대답을 추가하면 버튼을 클릭 할 때 논리를 배치하기 위해 다른 버튼을 활성화해야한다고 생각합니다. 당신의 코드로 버튼을 생성하기 위해

:

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView 
{ 
    return YES; 
} 
:

- (IBAction)button1 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" 
                 message:@"Do you like smoking?" 
                 delegate:self 
               cancelButtonTitle:@"Yes" 
               otherButtonTitles:@"No", nil]; 
    [alert show]; 
} 

그러나 클릭되는 버튼을 아는 전에,이 대리자 메서드를 호출하여 첫 ​​번째 다른 버튼을 활성화해야

사용자가 만든 NO 버튼이 활성화됩니다.그러면 clickedButtonAtIndex 메서드로 일부 논리를 수행 할 수있게 될 것입니다.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 0) { 
     // i love smoking! 
    } else if (buttonIndex == 1) { 
     // i hate smoking! 
    } 
} 

이 헤더 클래스의 UIAlertViewDelegate를 선언해야합니다 :

는 UIAlertView의 대리자 메서드를 구현합니다.

alertViewShouldEnableFirstOtherButton : 메서드가 YES를 반환하는지 확인하십시오. 그렇지 않으면 버튼을 눌렀을 때 논리를 입력 할 수 없습니다.

희망이 도움이됩니다. :)

+0

고맙게도 코드는 완전히 작동 하나이 코드는 작동하지 않습니다 :) –