gpt4 book ai didi

objective-c - 从模态呈现的 Controller 中呈现 UIAlertController,该 Controller 将被关闭

转载 作者:太空狗 更新时间:2023-10-30 03:53:55 25 4
gpt4 key购买 nike

在 iOS 8 之前,UIAlertView 可以从模态呈现的 UIViewController 中显示,同时 UIViewController 被关闭。我发现这在用户需要提醒他们按下模态呈现的 Controller 上的“保存”按钮时发生的某些更改时特别有用。从 iOS 8 开始,如果 UIAlertController 在被关闭时从模态呈现的 View Controller 中显示,则 UIAlertController 也会被关闭。 UIAlertController 在用户可以阅读或自己关闭之前被关闭。我知道我可以让模态呈现的 Controller 的委托(delegate)在 Controller 被关闭后显示警报 View ,但是这种情况会产生大量额外的工作,因为这个 Controller 在很多地方都使用,并且 UIAlertController 必须在某些条件下呈现,在每种情况下都需要将参数传递回 Controller 委托(delegate)。有没有什么方法可以在关闭 Controller 的同时从模态呈现的 Controller (或至少从 Controller 内的代码)显示 UIAlertController,并让 UIAlertController 一直保持到它被关闭?

最佳答案

您可以在模态 Controller 类的 dismissViewControllerAnimated 方法的完成 block 中处理此问题。在应该在任何类中处理的 rootviewcontroller 上呈现 UIAlertController。

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.navigationItem.rightBarButtonItem setAction:@selector(dismissView)];
[self.navigationItem.rightBarButtonItem setTarget:self];
}
- (void)dismissView {
[self dismissViewControllerAnimated:YES completion:^{
[self showAlert];
}];
}

- (void)showAlert {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert" message:@"This is Alert" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okButton = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[alertController dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
[alertController dismissViewControllerAnimated:YES completion:nil];
}];
[alertController addAction:okButton];
[alertController addAction:cancelButton];
UIViewController *rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;
[rootViewController presentViewController:alertController animated:YES completion:nil];
}

关于objective-c - 从模态呈现的 Controller 中呈现 UIAlertController,该 Controller 将被关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28891723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com