2013-02-02 2 views
2

터치 한 경우 "Cancel"및 "Yes"버튼이있는 경고 메시지가 팝업되는 메뉴에 버튼이 있습니다.버튼에 대한 iOS 알림보기 동작

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Exit game" 
               message:@"Are you sure?" 
               delegate:nil 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"Yes", nil]; 
[alert show]; 

는 버튼 "Yes"에 액션을 추가 할 수 있습니다 : 이것은 내가 경고에 대해 가지고있는 코드는?

답변

11

는 UIAlertView의 위임을 설정

다음과 같이 동일한 클래스의 대리자 함수를 작성, 자기에 대리자를 설정 한 것처럼
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Exit game" message:@"Are you sure?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil]; [alert show]; 

:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
if (buttonIndex == 1) { // Set buttonIndex == 0 to handel "Ok"/"Yes" button response 
    // Cancel button response 
    }} 
1

당신은

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 1) { 
     // do stuff 
    } 
} 
+0

그리고 만약 여러 개의 UIAlertView를 가지고 있다면 어느 태그가 호출되는지를 묘사하는 태그를 설정해야합니다. –

0

예는 쉽습니다 ... UIAlertViewDelegate

를 구현하고 다음을 추가해야합니다. 지금 "nil"로 설정 한 "delegate"라는 인수를 참조하십시오. 개체에 설정 ... 일반적으로보기 컨트롤러에서 호출하고 UIAlertViewDelegate에 대한 선택기를 구현하는 경우 "self".

또한보기 컨트롤러가 UIAlertViewDelegate 프로토콜을 준수 함을 선언해야합니다. 이를 수행하기위한 좋은 장소는 뷰 컨트롤러의 "개인"연속 클래스입니다. 코드에서

@interface MyViewController() <UIAlertViewDelegate> 
@end 

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