ios - 带有标签的多个 AlertView,无法取消

标签 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];
}
}

最佳答案

您可以仅为 UIAlertview 设置 TAG ,但您可以使用按钮 Index 识别 button/p>

- (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

关于ios - 带有标签的多个 AlertView,无法取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31870003/

相关文章:

ios - 触发 segue 时出现错误 "fatal error: unexpectedly found nil while unwrapping an Optional value"

iphone - 将 UIVIew 作为 SubView 添加到窗口 iPhone 的右下角

objective-c - CALayer - 未调用显示方法

objective-c - 带有&符号的文本文件被绘制到电子邮件消息停止模板中

SwiftUI - 如何仅在一个屏幕上自定义导航栏?

ios - 如何使用来自两个单独的 Alamofire 请求的数据初始化一个对象?

ios - UIVisualEffectView 和 UITableView

objective-c - 使用 NSTask 执行 ionic 构建命令 - 无法访问启动路径

iPhone:objective-c 中的音频分析

ios - 安装 Xcode 8 测试版