2011-02-06 38 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 2013-02-26 13:53:51

+4

使用取消按鈕索引可能更清晰:`[myAlertView dismissWithClickedButtonIndex:myAlertView.cancelButtonIndex animated:YES];`。 – 2015-02-09 09:23:50

5

由於您使用的UIAlertView委託回調方法,我認爲這是更好,你使用下面的代碼

[myAlertView dismissWithClickedButtonIndex:0 animated:YES]; 

如果沒有則使用上面的推薦代碼

[myAlertView dismissWithClickedButtonIndex:-1 animated:YES];