2011-02-06 5 views
18

어떻게하면 UIAlertView를 닫을 수 있습니까? 이 코드는 작동하지 않습니다.어떻게 UIAlertView를 닫을 수 있습니까?

@property (nonatomic, retain) UIAlertView *activityAlertView; 
- (void)viewDidLoad 
{ 
self.activityAlertView = [[UIAlertView alloc] initWithTitle:@"Receiving data" message:@"\n\n" 
                 delegate:self 
               cancelButtonTitle:@"Cancel" 
               otherButtonTitles:nil, nil]; 
[activityAlertView show]; 
} 

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

-(void) myfunc 
{ 
[self alertView:activityAlertView clickedButtonAtIndex:1]; 
} 

답변

56

UIAlertView 클래스의 - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated 메서드는 원하는대로 처리합니다. 예 :

[myAlertView dismissWithClickedButtonIndex:-1 animated:YES]; 
+1

Thnx Buddy ... :-) – Ayaz

+4

취소 버튼 색인을 사용하는 것이 더 깔끔합니다 :'[myAlertView dismissWithClickedButtonIndex : myAlertView.cancelButtonIndex animated : YES];'. –

+0

@VincentTourraine 오, 당신이 거기에서 파고 드린 오래된 대답. 취소 버튼 색인이 올바른 경우 원하는 동작에 따라 다릅니다. 경고를 무시하고 싶다면 '-1'을 선택하십시오. 취소 사례를 통해 실행하는 것이 좋으면 취소 버튼 색인이 올바른 것입니다. – JustSid

5

당신이 UIAlertView의 대리인 콜백 메소드를 사용하고 있기 때문에, 나는 다음과 같은 코드를 사용하여 더 나은 생각 다음 위의 제안 코드

[myAlertView dismissWithClickedButtonIndex:-1 animated:YES]; 
를 사용하지 않을 경우

[myAlertView dismissWithClickedButtonIndex:0 animated:YES]; 

+1

dismissWithClickedButtonIndex – Gank

관련 문제