多个AlertView带标签,无法取消

时间:2015-08-07 05:02:36

标签: ios objective-c xcode uialertview

我搜索了Stack实现多个alertViews的方法。大多数答案都是使用标签。这种方式效果很好,除了一个巨大的东西 - 取消按钮!当alertView弹出时,无论您点按“取消”还是“ yourButtonTitle ”,您的操作都会完成。有没有办法取消带有标签的alertView

这是我的代码:

#define TAG_ONE 1
#define TAG_TWO 2

- (IBAction)someButton1 {

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Call" message:@"Call number?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Call", nil];

alertView.tag = TAG_ONE;
[alertView show];
}

- (IBAction)someButton2 {

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Log Out?" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil];

alertView.tag = TAG_TWO;
[alertView show];
}

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

if (alertView.tag == TAG_ONE) { 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:101-101-1010"]];

} else if (alertView.tag == TAG_TWO){ 
    [PFUser logOut];
    [self performSegueWithIdentifier:@"showLogin" sender:self];
}
}

2 个答案:

答案 0 :(得分:1)

您只能为TAG设置UIAlertview,但您可以使用button

标识Index
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

if (alertView.tag == TAG_ONE) {
if(alertView.cancelButtonIndex == buttonIndex){
// Do cancel
}
else{
// Do the success thing
}
}
else if (alertView.tag == TAG_TWO) {
// same thing followed
}
}

buttonIndex == 0 //确定 buttonIndex == 1 //取消

额外Reference

答案 1 :(得分:0)

在询问if (alertView.cancelButtonIndex == buttonIndex){ // Do cancel }

之前,您必须先添加if (alertView.tag == TAG_ONE) {