gpt4 book ai didi

iOS/objective-C : Call AlertViewController from sharedInstance

转载 作者:行者123 更新时间:2023-12-01 19:43:34 27 4
gpt4 key购买 nike

我正在将一些自 iOS 9.0 以来已弃用的 UIAlertViews 更新为 UIAlertViewControllers。

使用 UIAlertView,可以从任何正在执行的代码中抛出警报——即使是在实用程序类或共享实例中——只需一行:

[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,这是不允许的。如果您将以下代码放在可通过共享实例访问的类的方法中,当您到达 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];

在最后一行,该类(通过共享实例访问)没有此方法。看来您必须使用更复杂的方式来引发警报。我看过一些 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。任何人都可以提出一个简单、直接的方法来做到这一点吗?

最佳答案

我在 UIViewController 上创建了一个扩展,它允许我创建一个新窗口并从那里呈现一个 View Controller 。这使我可以从任何类进行演示,而不仅仅是 View Controller 。此外,它还可以防止您尝试从已经呈现 View Controller 的 View Controller 显示警报 View 的问题。

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
}
}

然后,您可以使用此方法来呈现您的警报 Controller (或任何 UIViewController):
alertViewController.presentFromNewWindow()

当您关闭警报 View Controller 时,它会从 View Controller 中移除,并且窗口也会从层次结构中移除。

关于iOS/objective-C : Call AlertViewController from sharedInstance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51970459/

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