如何在沒有視圖控制器的情況下顯示UIAlertController?場景:用戶點擊視圖控制器上的按鈕。視圖控制器是導航堆棧中的最頂層(顯然)。TAP調用另一個類上調用的實用程序類方法。在那里發生了一件壞事,我想在控件返回到視圖控制器之前在那里顯示一個警告。+ (void)myUtilityMethod {
// do stuff
// something bad happened, display an alert.}這是有可能的UIAlertView(但可能不太恰當)。在這種情況下,您如何表示UIAlertController,就在那里myUtilityMethod?
3 回答

BIG陽
TA貢獻1859條經驗 獲得超6個贊
let alertController: UIAlertController = ...UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
let alertController: UIAlertController = ...UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil)

梵蒂岡之花
TA貢獻1900條經驗 獲得超5個贊
斯威夫特
let alertController = UIAlertController(title: "title", message: "message", preferredStyle: .alert)//...var rootViewController = UIApplication.shared.keyWindow?.rootViewControllerif let navigationController = rootViewController as? UINavigationController { rootViewController = navigationController.viewControllers.first}if let tabBarController = rootViewController as? UITabBarController { rootViewController = tabBarController.selectedViewController}//...rootViewController?.present(alertController, animated: true, completion: nil)
目標-C
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];//...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:alertController animated:YES completion:nil];
- 3 回答
- 0 關注
- 1174 瀏覽
添加回答
舉報
0/150
提交
取消