亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在沒有視圖控制器的情況下顯示UIAlertController?

如何在沒有視圖控制器的情況下顯示UIAlertController?

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

3 回答

?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

您可以使用SWIFT 2.2執行以下操作:

let alertController: UIAlertController = ...UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)

以及SWIFT 3.0:

let alertController: UIAlertController = ...UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil)


查看完整回答
反對 回復 2019-06-29
?
梵蒂岡之花

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];


查看完整回答
反對 回復 2019-06-29
  • 3 回答
  • 0 關注
  • 1174 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號