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

BIG陽(yáng)
TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
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貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊
斯威夫特
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)
目標(biāo)-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 關(guān)注
- 1183 瀏覽
添加回答
舉報(bào)
0/150
提交
取消