2012-08-09 86 views
7

如何以编程方式取消UIAlertView? 这是我用来显示UIAlertView中以编程方式取消UIAlertView

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Save Background" message:@"Downloading..." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil]; 
    CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 70.0); 
    [myAlertView setTransform:myTransform]; 
    [myAlertView show]; 

回答

11

尝试代码使用dismissWithClickedButtonIndex:这样的:

下面是使用代码:

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

+1对你来说,比我输入时快20秒[['''cancelButtonIndex'“](https://developer.apple.com/library/ios/documentation/UIKit/Reference/不过,UIAlertView_Class/UIAlertView/UIAlertView.html#// apple_ref/occ/instp/UIAlertView/cancelButtonIndex)并不总是“-1”。 – 2012-08-09 16:26:22

4

你可以通过

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated 

同时调用“取消”按钮索引。

您也可以简单地从超级视图中删除警报视图(即“removeFromSuperview”)。如果您不使用ARC,请确保“release”为已分配的视图。