2016-03-27 12 views
0

インターネットに接続されていない場合にポップアップするUIAlertControllerがあります。私は、接続が戻ってきたときにそれを正しく却下する方法を知らない。私は私がしなければ、それを10秒毎にチェックしておくよう:アラートビューがすでに解任された後UIAlertControllerを閉じることができません

self.dismissViewControllerAnimated(true, completion: nil) 

これは、すべての間隔でメインビューを閉じます。私が試した:

let alert = UIAlertController(title: "No internet Connection", message: "Make sure your device is connected to the internet.", preferredStyle: UIAlertControllerStyle.Alert) 

    if Reachability.isConnectedToNetwork() == true { 
     print("Internet connection OK") 

     alert.dismissViewControllerAnimated(true, completion: nil) 

    } else { 
     print("Internet connection FAILED") 

     self.presentViewController(alert, animated: true, completion: nil) 
    } 

が、alert.dismissViewControllerAnimated(true, completion: nil)を動作しません。 ありがとう!

+0

ありがとうユーザーはそれらを却下することができますか? – Sweeper

+0

アラートを表示する前にアラートを却下しようとしている可能性があります。 – HardikDG

答えて

1

私は、問題が複数回UIAlertController提示すると... UIAlertControllerの1つのインスタンスを解任しようとしていると思いますので、私は考えて

let alert = UIAlertController(title: "No internet Connection", message: "Make sure your device is connected to the internet.", preferredStyle: UIAlertControllerStyle.Alert) 

    if Reachability.isConnectedToNetwork() == true { 
     print("Internet connection OK") 

     alert.dismissViewControllerAnimated(true, completion: nil) 

    } else { 
     print("Internet connection FAILED") 
     alert.dismissViewControllerAnimated(true, completion: nil) 
     self.presentViewController(alert, animated: true, completion: nil) 
    } 
+0

あなたは何を意味しているのですか?提示する前にそれを却下しても機能しません。 – Nch

+0

時間間隔を10秒から30秒に増やしてチェックしてください。それ以外の場合は、単一のインスタンスUIAlertControllerを使用して、アニメーションなしでプレゼンテーションやディスプレイを試すことができます。 alert.dismissViewControllerAnimated(false、completed:nil) –

+0

なぜあなたはダウン投票しましたか?あなたはそれを説明することができます.. –

0

の下のような存在の前にそれをUIAlertControllerを却下する必要がありますあなたが唯一のインターネットの後にアラートを却下したい場合は、バック「UIの参照を保持さ :問題はあなたが警告

アップデートを表示する前に警告を解任しようとしていることができあなたがそのオブジェクトでそれを解除することができたら表示された後のAlertController '

あなたのケースでは、表示されていない新しく作成されたオブジェクトの'警告 'を却下しようとしています。インターネットが帰ってきたときに新しいものを作成する前に参考にしてください

また、10秒ごとにメソッドを呼び出すと、新しいアラートを避けるために新しいアラートを提示する前に古いアラートを却下します。それ以外の場合は、アラートがすでに表示されている場合アラートが終了するまで10秒ごとにもう一度チェックしません

より良いオプションは: インターネットのco nnect外とあれば何のインターネット接続がのみ作成しないUIAlertController

あなたはグローバル変数として警告作り、あなたがインターネット接続が戻ったとき、他のブロックに、あなたが警告かどうかを確認することができますことを作成するときに、アラートオブジェクトを格納することができます1更新はいアラートに

if Reachability.isConnectedToNetwork() == true { 
     print("Internet connection OK") 
     if alert != nil { 
      alert.dismissViewControllerAnimated(true, completion: nil) 
     } 
    //dismiss the alert using old refence from the alert 
    //continue your normal code 
    } else { 
     alert = UIAlertController(title: "No internet Connection", message: "Make sure your device is connected to the internet.", preferredStyle: UIAlertControllerStyle.Alert) 
     //you may check and dismiss the previous alertcontroller before presenting the new one 
     print("Internet connection FAILED") 
     self.presentViewController(alert, animated: true, completion: nil) 
    } 
+0

ありがとう!接続が戻っていない限り、ユーザーはokを押して警告を閉じることはできません。 – Nch

+0

elseブロックで宣言する前にアラートを閉じることはできません – Nch

+0

ありがとうございました!しかし、宣言する前にalert!= nilをチェックすることはできません。 – Nch

0

を消すことができます場合、これは私のために働いたオブジェクトにあります。@OS_Binodと@Pyro

はたぶん、あなたは、インターネットconnがあり_is_と言って別のビューコントローラを表示することができ
 let alert = UIAlertController(title: "No internet Connection", message: "Make sure your device is connected to the internet.", preferredStyle: UIAlertControllerStyle.Alert) 

    if Reachability.isConnectedToNetwork() == true { 
     print("Internet connection OK") 

     self.dismissViewControllerAnimated(false, completion: nil) 

    } else { 
     print("Internet connection FAILED") 
     alert.dismissViewControllerAnimated(false, completion: nil) 
     self.presentViewController(alert, animated: false, completion: nil) 
    } 
関連する問題