简体   繁体   English

UIAlertView不会触发方法

[英]UIAlertView won't trigger method

I have an option in my iOS Newsstand application for users to delete all of the issues in the app to free up space. 我的iOS书报摊应用程序中有一个选项供用户删除应用程序中的所有问题以释放空间。 Initially one press of the button would delete everything without warning so I decided to add in an alert view to give them the chance to delete or cancel. 最初,一按该按钮会删除所有内容而不会发出警告,因此我决定添加一个警报视图,以使他们有机会删除或取消。

I have the alert working just how I want it but choosing "delete" doesn't trigger the method. 我的警报按我的需要工作,但是选择“删除”不会触发该方法。 Can anyone tell me why? 谁能告诉我为什么?

This is my code: 这是我的代码:

- (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];
}

When you create your UIAlertView you never assign it a tag of 100 so when you do the check if (alertView.tag == 100) in clickedButtonAtIndex: it will always return FALSE and never make it to your second if statement where you determine which button was pressed. 当您创建UIAlertView您永远不会为其分配标签100因此在if (alertView.tag == 100)进行检查if (alertView.tag == 100)clickedButtonAtIndex:它将始终返回FALSE并且永远不会返回到您确定哪个按钮的第二条if语句中被按下。 So change your showAlert too: 因此,也请更改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];
}

Personally I'd actually create a constant and assign it the constant and check that so have a constant like const int deleteAllAlertTag = 100; 我个人实际上会创建一个常量并将其分配给常量,并检查是否具有常量,例如const int deleteAllAlertTag = 100; declared at the top of you implementation file then have [alert setTag:deleteAllAlertTag]; 在实现文件顶部声明的名称,然后具有[alert setTag:deleteAllAlertTag]; and then you can do if (alertView.tag == deleteAllAlertTag) . 然后可以执行if (alertView.tag == deleteAllAlertTag) I'd do this only because if you decide to change the value of your alertView tag you only have to change it once and your code will still work. 我之所以这样做,是因为如果您决定更改alertView标记的值,则只需更改一次即可,并且代码仍然可以正常工作。

You need 你需要

alert.tag = 100;

in the code where you create it. 在创建它的代码中。 If this is in fact the problem, then your question isn't why the method isn't tiggered, since it is. 如果这实际上是问题所在,那么您的问题不在于为什么该方法不会被触发,因为它确实存在。 Using the debugger to step through your code would show the if failing to trigger because the tag isn't set to 100. 使用调试器单步执行代码将显示if由于标签未设置为100而触发失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM