1

私はNavigationControllerを持っています。 ThirdViewController私はいくつかのタスクを実行していますが、失敗すると、UIAlertControllerを使用して警告メッセージを表示します。ViewControllerがウィンドウ階層にないときに警告を表示しない

タスクを開始してSecondViewControllerに戻ると、エラーメッセージがSecondViewControllerに表示され、[OK]をクリックするとすべてがナビゲーションバーの下に黒く表示されます。私はナビゲーションバーのみを残しており、FirstViewControllerに再び戻ると、ナビゲーションバー以外は同じ黒い表示になります。

プレゼンテーションウィンドウ階層にないViewControllerの警告が問題を作成します。私がスクリーン上にいないと警告を表示したくない。

ViewControllerをゆっくりとスワイプすると簡単に再現できます。

これを処理する最善の方法は何ですか?あなたは私たちがそこに起こる内容を正確に把握していない任意のコードを共有していなかったので

ThirdViewController

func buttonTapped() { 
     APIManager.sharedManager.getDetails(completion: { (details ,error) -> Void in 
      guard error == nil else { 
       Alert.errorMsg(error!.localizedDescription, viewController: self, goBack: false) 
       return 
      } 
      print(details) 
     } 
    } 

class Alert: NSObject { 

    /* Error message */ 
    class func errorMsg(message: String, viewController: UIViewController?, goBack: Bool = false) { 
     let alertView = UIAlertController(title: "Error", message: message, preferredStyle: .Alert) 
     let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (alert: UIAlertAction) -> Void in 
      if goBack == true && viewController != nil { 
       viewController!.navigationController?.popToRootViewControllerAnimated(true) 
      } 
     } 
     alertView.addAction(action) 
     let controller = viewController ?? UIApplication.sharedApplication().keyWindow?.rootViewController 
     controller!.presentViewController(alertView, animated: true, completion: nil) 
    } 
} 
+1

もっとアイデアを得るためにコードを投稿してください – Lion

答えて

3

私はCustomViewControllerを作成し、プロパティ 'isUnloading' を追加するのに役立ちます。 viewWillDisappearでは、isUnloading = trueと設定しました。私はアラートを提示する前にプロパティをチェックします。

0

に私のコード、

ボタンのアクションを共有します。しかし、ビューコントローラがウィンドウ階層にない場合に警告を表示したくない場合は、警告ビューを表示する前にviewController.view.windowが設定されているかどうかを確認し、設定されている場合にのみ表示することができます。

あなたのような何かを行うことができます
0

、などの警告と呼ばれる

class AlertHelper { 
func showAlert(fromController controller: UIViewController) { 
    var alert = UIAlertController(title: "abc", message: "def", preferredStyle: .Alert) 
    controller.presentViewController(alert, animated: true, completion: nil) 
} 
} 

は、

var alert = AlertHelper() 
alert.showAlert(fromController: self) 

は詳細について this linkを参照してください。

希望はこれが:)

関連する問題