2011-08-03 3 views
14

이전에이 작업을 수행했음을 알고 있었지만 다시는 이해할 수 없습니다.UIAlertView 취소 버튼을 선택하면 쉽게 알 수 있습니다.

취소 버튼을 눌렀는지 확인하는 방법은 무엇입니까? 버튼 인덱스를 기반으로하고 싶지 않습니다. 다음과 같이 할 수있는 방법이 있습니다.

[alertView isCancelIndex:index]; 

누구나 알고 계십니까?

+8

질문에 그런 종류의 쉽게 문서에 의해 해결된다. 당신은 또한 많은 시간을 절약 할 수 있습니다. UIAlertView 클래스 레퍼런스를 찾기 위해, 애플의 링크를 클릭하고, 프로퍼티와 메소드 목록을 조금 아래로 스크롤하는 것만 큼 쉽습니다. – EmilioPelaez

답변

52

UIAlertView는 UIAlertView의 대리자 버튼 인덱스

@property(nonatomic) NSInteger cancelButtonIndex 

사용

[alertView cancelButtonIndex] 
2

취소의 특성을 상기 방법을한다 갖는다

(무효) alertView (UIAlertView *) alertView clickedButtonAtIndex : (NSInteger) buttonIndex

: 그리고

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSInteger cancelIndex = [alertView cancelButtonIndex]; 
    if (cancelIndex != -1 && cancelIndex == buttonIndex) 
    { 
     // Do something... 
    } 
} 
+0

OP는 구체적으로 그 방법을 사용하지 않는다고 말했습니다. – rich

+2

OP는 그가 색인을 하드 코딩하고 싶지 않다는 것을 의미한다고 생각합니다. (그가 의미하는 것을 알고있는 범위까지). –

32
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 

    if (buttonIndex == [alertView cancelButtonIndex]) { 
    NSLog(@"The cancel button was clicked for alertView"); 
    } 
// else do your stuff for the rest of the buttons (firstOtherButtonIndex, secondOtherButtonIndex, etc) 
} 

관련 문제