2016-12-16 4 views
0

サービスに接続し、バックエンドからデータを受信して​​います。コードは次のとおりです。アラートコントローラがメインスレッドでフリーズする

_ = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) in 

     if response != nil{ 

      var responseREcvd = response as? NSHTTPURLResponse 
      if responseREcvd?.statusCode == 404 { 
       let alertControler = UIAlertController(title: "404", message: "Server Down.", preferredStyle: .Alert) 
       let alertAction = UIAlertAction(title: "OK", style: .Default, handler: nil) 
       alertControler.addAction(alertAction) 
       dispatch_async(dispatch_get_main_queue(), { 
        targetVC.presentViewController(alertControler, animated: true, completion: nil) 
       }) 
      } 
     } 

アラートが表示されますが、UIがフリーズします。 OKアクションボタンを押すと、アクションは呼び出されません。

+0

はこれです問題は解決しましたか? –

+0

@USER_NAMEはい問題は修正されました。問題は、画面に隠れていたuiviewのインスタンスがもう1つあったことです。アラートコントローラの[OK]ボタンを押すことができるように、それを削除しなければなりませんでした。 –

答えて

0

extension UIAlertController { 

    func show() { 
     present(true, completion: nil) 
    } 

    func present(
     animated: Bool, completion: (() -> Void)?) { 
     if let rootVC = UIApplication.sharedApplication().keyWindow?.rootViewController { 
      presentFromController(rootVC, animated: animated, completion: completion) 
     } 
    } 

    private func presentFromController(controller: UIViewController, animated: Bool, completion: (() -> Void)?) { 
     self.addAction(UIAlertAction.init(title: "Ok", style: .Default, handler: { (true) in 
      self.dismissViewControllerAnimated(true, completion: nil) 
     })) 
     if let navVC = controller as? UINavigationController, 
      let visibleVC = navVC.visibleViewController { 
      presentFromController(visibleVC, animated: animated, completion: completion) 
     } else 
      if let tabVC = controller as? UITabBarController, 
       let selectedVC = tabVC.selectedViewController { 
       presentFromController(selectedVC, animated: animated, completion: completion) 
      } else { 
       controller.presentViewController(self, animated: animated, completion: completion); 
     } 
    } 
} 

使用この拡張機能を試してみてください:

let alertController = UIAlertController(title: "At \((arr_day.objectAtIndex(index.row - 1) as! String)) End Time Missing", message: "", preferredStyle: .Alert) 
alertController.show() 

希望これはあなた

+0

UIがフリーズします。ノータッチが認識されます。私はあなたの解決策を試みましたが、運はありませんでした。 –

+0

@PrabhakarPatilもし私があなたに私がalertviewControllerのために使った拡張を与えることができますか? Uは警告を表示するためにその拡張機能を呼び出す必要があります。そしてなぜalertviewcontrollerを派遣したのですか? –

+0

私は拡張機能を試してみましょう、親切に共有 –

0

のために動作しますがNSOperationQueueを使用しよう:

_ = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) in 

    if response != nil{ 

     var responseREcvd = response as? NSHTTPURLResponse 
     if responseREcvd?.statusCode == 404 { 
      let alertControler = UIAlertController(title: "404", message: "Server Down.", preferredStyle: .Alert) 
      let alertAction = UIAlertAction(title: "OK", style: .Default, handler: nil) 
      alertControler.addAction(alertAction) 
      dispatch_async(dispatch_get_main_queue(), {NSOperationQueue.mainQueue().addOperationWithBlock { 
       targetVC.presentViewController(alertControler, animated: true, completion: nil)} 
      }) 
     } 
    } 
関連する問題