首页 > 解决方案 > 为什么出现打开 iTunes App Store url 时 web 视图中的取消按钮不起作用。(为了理解)

问题描述

是因为没有实现取消按钮的方法吗?

用蓝色圈住的取消按钮不起作用

下面是我的代码。通过警报视图通知用户有可用的更新版本。如果单击“确定”,它会将它们定向到 iTunes App Store 中的应用程序。(如下图所示)但是当点击取消时,什么也没有发生。

-(void)showAlert {

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"New Version Available on Appstore" message:@"Please update app to continue" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        UIApplication *application = [UIApplication sharedApplication];
        NSURL *iTunesLink = [NSURL URLWithString:@"itms://itunes.apple.com/us/app/apple-store/id375380948?mt=8"];
        [application openURL:iTunesLink options:@{} completionHandler:^(BOOL success) {
            if (success) {
                 NSLog(@"Opened url");
            }
        }];
            //button click event
                        }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:ok];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

我设法通过添加“itms-apps”来解决这个问题(直接重定向到应用商店,没有取消按钮)。但我仍然想了解为什么取消按钮不起作用

由于网络视图中的取消按钮不是我创建的,是否可以为其添加方法?

对于我的问题不清楚,我深表歉意。我想知道为什么屏幕截图中带圆圈的取消按钮不起作用。

标签: iosobjective-ciphoneapp-storeios11

解决方案


当您创建一个 UIAlertController 时,您可以将不同的按钮添加到它作为 UIAlertActions,当每个按钮被点击时它们会做一些事情。您必须通过在 UIAlertAction 的初始化中实现处理程序来指定您想要发生的事情(就像您在上面对 OK 操作所做的那样)。

如果您将 nil 作为处理程序传递,则警报将被简单地解除,但不会发生任何其他事情,因为您没有告诉它做任何其他事情。

取消按钮与单击警报按钮有关,而不是您单击其他按钮后所做的任何事情,就像您可能希望的那样。

更多信息在这里


推荐阅读