ios - 为什么出现打开 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”来解决这个问题(直接重定向到应用商店,没有取消按钮)。但我仍然想了解为什么取消按钮不起作用
由于网络视图中的取消按钮不是我创建的,是否可以为其添加方法?
对于我的问题不清楚,我深表歉意。我想知道为什么屏幕截图中带圆圈的取消按钮不起作用。
解决方案
当您创建一个 UIAlertController 时,您可以将不同的按钮添加到它作为 UIAlertActions,当每个按钮被点击时它们会做一些事情。您必须通过在 UIAlertAction 的初始化中实现处理程序来指定您想要发生的事情(就像您在上面对 OK 操作所做的那样)。
如果您将 nil 作为处理程序传递,则警报将被简单地解除,但不会发生任何其他事情,因为您没有告诉它做任何其他事情。
取消按钮与单击警报按钮有关,而不是您单击其他按钮后所做的任何事情,就像您可能希望的那样。
更多信息在这里。
推荐阅读
- linux - Linux 调度程序在内存不足时的行为
- java - 有没有一种方法可以在继承中返回某种类型的列表?
- java - Tomcat 无法设置数据源连接的属性
- java - Spring Boot 微服务作为 Kafka 消费者异常
- java - 一台服务器中的多个 couchbase 实例
- visual-studio-code - 如何删除终端 xterm 文本层画布的填充颜色?
- python - 用我的字符串标签填充 matplotlib 饼图扇区
- php - Guzzle 查询字符串参数
- sapui5 - 具有托管应用程序路由器的 SAP Cloud Foundry 多租户
- python - Unicode 字符串格式,因此文本会自动拆分为 PowerPoint 表格中的列