iOS/Objective-C: 从 sharedInstance 调用 AlertViewController

iOS/Objective-C: Call AlertViewController from sharedInstance

我正在更新一些 UIAlertViews,从 iOS 9.0 开始弃用到 UIAlertViewControllers。

使用 UIAlertView,可以从任何正在执行的代码中抛出警报——即使在实用程序 class 或共享实例中——使用简单的行:

[alertView show];

所以如果我调用共享实例,例如

- (void)didTapDeleteButton:(id)sender {
    NSArray *itemsToDelete = [self.selectedIndexPathToContact allValues];

    [[IDModel sharedInstance] deleteItems:itemsToDelete];

//其中包含代码:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Keep Archive Copy?"
                                                    message:nil
                                                   delegate:self
                                          cancelButtonTitle:@"No"
                                          otherButtonTitles:@"OK",nil];
alertInvite.alertViewStyle = UIAlertViewStyleDefault;
alertInvite.tag=100;
[alertView show];

一切正常。

但是,对于 UIAlertController,这是不允许的。如果将以下代码放在可通过共享实例访问的 class 的方法中,当您到达 presentViewController 时,它会抛出错误:

UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"Delete Item?" message:nil preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* yesButton = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
    [alertView dismissViewControllerAnimated:YES completion:nil];
}];

UIAlertAction* noButton = [UIAlertAction actionWithTitle:@"Not Now" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
    [alertView dismissViewControllerAnimated:YES completion:nil];
}];

[alertView addAction:noButton];
[alertView addAction:yesButton];
if ([alertView respondsToSelector:@selector(setPreferredAction:)]) {
    [alertView setPreferredAction:yesButton];
}
//FOLLOWING THROWS ERROR
[self presentViewController:alertView animated:YES completion:nil];

在最后一行,class(通过共享实例到达)没有这个方法。看来您必须使用更复杂的方法来发出警报。我见过一些 somwehat convoluted approaches 如下所示:

id rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
    rootViewController = ((UINavigationController *)rootViewController).viewControllers.firstObject;
}
if([rootViewController isKindOfClass:[UITabBarController class]])
{
    rootViewController = ((UITabBarController *)rootViewController).selectedViewController;
}
[rootViewController presentViewController:alertInvite animated:YES completion:nil];

但是,这对我不起作用,因为我认为我的共享实例没有 rootviewcontroller。谁能建议一个简单、直接的方法来做到这一点?

我相信你的问题很含糊。但我认为您一直在寻找的是呈现 UIAlertController 的示例。那是对的吗?如果是,请继续阅读...

示例:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction: [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:nil]];
alert.TitleColor = [UIColor whiteColor];
[self presentViewController:alert animated:YES completion:nil];

文档:https://developer.apple.com/documentation/uikit/uialertcontroller

显示来自我能想到的任何代码的警报:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction: [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:nil]];
alert.TitleColor = [UIColor whiteColor];

id<UIApplicationDelegate> delegate = [UIApplication sharedApplication].delegate;
UIViewController *vc = delegate.window.rootViewController;
[vc presentViewController:alert animated:YES completion:nil];

注:

请注意,在大多数情况下我不会这样做。
非 ui 代码不应该 ui!这可能也是苹果做出改变的部分原因:它鼓励适当的模型||视图分离

我在 UIViewController 上创建了一个扩展,允许我创建一个新的 window 并从那里呈现一个视图控制器。这允许我从任何 class 进行展示,而不仅仅是一个视图控制器。此外,它还可以防止您尝试从已经呈现视图控制器的视图控制器显示警报视图的问题。

extension UIViewController {
    func presentFromNewWindow(animated: Bool = true, completion: (() -> Void)? = nil) {
        let window = newWindow()

        if let rootViewController = window.rootViewController {
            window.makeKeyAndVisible()
            rootViewController.present(self, animated: animated, completion: completion)
        }
    }

    private func newWindow() -> UIWindow {
        let window = UIWindow(frame: UIScreen.main.bounds)
        let rootViewController = UIViewController()
        rootViewController.view.backgroundColor = .clear
        window.backgroundColor = .clear
        window.rootViewController = rootViewController
        window.windowLevel = UIWindowLevelAlert

        return window
    }
}

然后您可以使用此方法来显示您的 Alert Controller(或任何 UIViewController):

alertViewController.presentFromNewWindow()

当您关闭警报视图控制器时,它会从视图控制器中删除,window 也会从层次结构中删除。