告警框(UIAlertView)

** IOS 開發(fā)中 Whose view is not in the window hierarchy 錯(cuò)誤的解決辦法 **

         在 IOS 開發(fā)當(dāng)中經(jīng)常碰到 whose view is not in the window hierarchy 的錯(cuò)誤,該錯(cuò)誤簡(jiǎn)單的說,是由于 "ViewController" 還沒有被加載,就調(diào)用該 ViewController 或者 ViewController 內(nèi)的方法時(shí),就會(huì)報(bào)這個(gè)錯(cuò)誤。
         
         在不同地方調(diào)用 ViewController,解決的方法也不太一樣。
         
         
         1. 在 一個(gè) ViewController 里面調(diào)用另外一個(gè) ViewController 是出現(xiàn)這個(gè)錯(cuò)誤:
         
         該錯(cuò)誤一般是由于在 viewDidLoad 里面調(diào)用引起的,解決辦法是轉(zhuǎn)移到 viewDidAppear 方法里面
         
         
         
         2. 在 AppDelegate.m 中調(diào)用遇到這個(gè)錯(cuò)誤
         
         解決辦法1:
         
         UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
         while (topRootViewController.presentedViewController)
         {
         topRootViewController = topRootViewController.presentedViewController;
         }
         
         //[topRootViewController presentViewController:yourController animated:YES completion:nil];
         //or
         [topRootViewController myMethod];
         
         解決辦法2:
         
         UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
         LoginViewController* loginViewController = [mainstoryboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
         [self.window makeKeyAndVisible];
         //[LoginViewController presentViewController:yourController animated:YES completion:nil];
         //or
         [LoginViewController myMethod];

** 警告框要寫在viewDidAppear中 **

    override func viewDidAppear(_ animated: Bool){
        super.viewDidAppear(animated)
        
        let alert = UIAlertController(title: "警告", message: "警告信息", preferredStyle: .alert)
        alert.addTextField(configurationHandler: {
            (textfield:UITextField!) -> Void in
            textfield.placeholder = "輸入姓名"
        })
        alert.addTextField { (textfield2:UITextField!)-> Void in
            textfield2.placeholder = "輸入密碼"
        }
        let cancleAct = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        let okAct = UIAlertAction(title: "確定", style: .default, handler: {
            action in
            print("確定.............>")
        })
        //按鈕使用“告警”樣式(文字顏色變紅,用來來警示用戶)
//        let detAct = UIAlertAction(title: "刪除", style: .destructive, handler: {
//            action in
//            print("刪除")
//        })
        
        alert.addAction(cancleAct)
        alert.addAction(okAct)
//        alert.addAction(detAct)
        self.present(alert, animated: true, completion: {
            print("彈出警告框")
        })
    }
//兩秒鐘后自動(dòng)消失
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
    self.presentedViewController?.dismiss(animated: false, completion: nil)
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容