UIAlertView 不会触发方法

UIAlertView won't trigger method

我的 iOS 报亭应用程序中有一个选项供用户删除应用程序中的所有问题以释放 space。最初按一下按钮会在没有警告的情况下删除所有内容,所以我决定添加一个警告视图,让他们有机会删除或取消。

我的警报按我想要的方式工作,但选择 "delete" 不会触发该方法。谁能告诉我为什么?

这是我的代码:

- (void)showAlert 
{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Delete all issues?"
                                                    message:@"Your issues will be deleted to free up space on your device but can be re-downloaded as long as you are subscribed."
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"Delete", nil];
    [alert show];
}

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // Is this my Alert View?
    if (alertView.tag == 100) {
        //Yes
        // You need to compare 'buttonIndex' & 0 to other value(1,2,3) if u have more buttons.
        // Then u can check which button was pressed.
        if (buttonIndex == 0) { 
            // 1st Other Button
        } else if (buttonIndex == 1) { 
            // 2nd Other Button
            [self performSelector:@selector(trashContent) withObject:nil afterDelay:0.01];
        }
    } else {
        //No
        // Other Alert View
    }
}

- (void)trashContent 
{
    if (NewsstandApp) {
        NKLibrary *nkLib = [NKLibrary sharedLibrary];
        NSLog(@"%@",nkLib.issues);
        [nkLib.issues enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            [nkLib removeIssue:(NKIssue *)obj];
        }];

        [self.publisher addIssuesInNewsstand];
    } else {
        for (Issue *issue in self.publisher.issues) {
            NSFileManager* fileManager = [NSFileManager defaultManager];
            NSError *error;
            [fileManager removeItemAtPath:issue.fileUrl error:&error];

            if (!error) {
                issue.downloadState = IssueStatusNone;
            }
        }
    }
    [self.collectionView reloadData];
}

你需要

alert.tag = 100;

在您创建它的代码中。如果这实际上是问题所在,那么您的问题不是为什么不触发该方法,因为它是。使用调试器逐步执行您的代码会显示 if 未能触发,因为标记未设置为 100。

当您创建 UIAlertView 时,您永远不会为其分配 100 标签,因此当您在 clickedButtonAtIndex: 中进行检查 if (alertView.tag == 100) 时,它将始终 return FALSE 并且永远不要在确定按下哪个按钮的第二个 if 语句中出现。所以也改变你的showAlert

- (void)showAlert 
{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Delete all issues?"
                                                    message:@"Your issues will be deleted to free up space on your device but can be re-downloaded as long as you are subscribed."
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"Delete", nil];
    [alert setTag:100]; // Add this line.
    [alert show];
}

我个人实际上会创建一个常量并为其分配常量并检查是否有一个像 const int deleteAllAlertTag = 100; 这样的常量在你的实现文件的顶部声明然后有 [alert setTag:deleteAllAlertTag]; 然后你可以做 if (alertView.tag == deleteAllAlertTag)。我这样做只是因为如果您决定更改 alertView 标记的值,您只需更改一次并且您的代码仍然有效。