2017-08-01 83 views
0

我在第二視圖控制器有一個UIActivityIndicatorView,當用戶在第一視圖中選擇按鈕Json將啓動,但在同一時間的應用程序將運行第二viewcontrollerUIActivityIndicatorView動畫我要當第一viewcontrollerJson已完成停止UIActivityIndicatorView第二視圖控制器(只要我想停,而在另一個viewcontroller隱藏UIActivityIndicatorView如何在swift 3中隱藏另一個視圖控制器中的UIActivityIndi​​catorView?

這裏是第一個視圖控制器

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 


    let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath as IndexPath)! 
    selectedCell.backgroundColor = UIColor.clear 
    selectedCell.tintColor = UIColor.clear 
    print("Chat Subject Has Benn Selected!") 
    let selectedItem = indexPath.row 
    print(selectedItem) 


    chatViewController.selectedChat = selectedItem 
    ///////////////////////Start Chat 
    print(EmailSignInViewController.id)  

    ////////////////////// Get chat Page 

    let urlPath3: String = "http://example/api?api_token=\(EmailSignInViewController.api_token)&id=\(EmailSignInViewController.id[selectedItem])" 


    print(urlPath3) 
    let url3: NSURL = NSURL(string: urlPath3)! 
    let request3: NSMutableURLRequest = NSMutableURLRequest(url: url3 as URL) 

    request3.httpMethod = "GET" 


    let queue3:OperationQueue = OperationQueue() 

    NSURLConnection.sendAsynchronousRequest(request3 as URLRequest, queue: OperationQueue.main) {(response, data, error) in 


     do { 
      let data = try Data(contentsOf: url3 as URL) 
      print(NSString(data: data, encoding: String.Encoding.utf8.rawValue)!) 

     } catch { 
      print(error.localizedDescription) 
     } 

    do { 

    if let jsonResult3 = try JSONSerialization.jsonObject(with: data!, options: []) as? Array<Any> { 

    for item in jsonResult3 { 
    print("item") 
    print(item) 

    if let dict = item as? NSDictionary { 
    if let type = dict.value(forKey: "type"){ 
    chatViewController.type.append(type as! Int) 

    print("type") 
    print("\([type])") 

     chattingViewController.checklist.append(type as! Int) 

    } 

    if let text = dict.value(forKey: "text"){ 

    chatViewController.text.append(text as!String) 


     chattingViewController.list.append(text as!String) 
    print("text") 
     print("\([text])") 


    } 
     if let created_at = dict.value(forKey: "created_at"){ 

       self.created.append(created_at as!String) 


      print("created_at") 
      print("\([created_at])") 
      print("\([created_at])") 


     } 



    } 
    } 

     print(jsonResult3) 

    print("ASynchronous\(jsonResult3)") 
     chatViewController.created_at = self.created 

     print("chat Page") 

    } 
    } catch let error as NSError { 
    print(error.localizedDescription) 
    } 

      self.performSegue(withIdentifier: "chatting", sender: nil) 

    } 

的代碼,你在我的代碼看的時候Json站RT用戶將移動到第二viewcontrollerUIActivityIndicatorView將啓動動畫和表演,我想在Json最後一行停止動畫和隱藏 ,這裏是第二視圖控制器必要的代碼

@IBOutlet weak var chattingWaiting: UIActivityIndicatorView! 
override func viewDidLoad() { 
    super.viewDidLoad() 

    chattingWaiting.startAnimating() 
    chattingWaiting.isHidden = false 

} 
+0

如果要添加第二個的viewController作爲孩子第一的viewController那麼你可以通過解僱第二的viewController的UIActivityIndi​​catorView [child.activityIndi​​cator stopAnimating] –

+0

我該怎麼做?請幫我 –

+0

請添加一些代碼來幫助你更好。 –

回答

0

更新: :您遇到的真正問題是您在導航發生後將UIActivityIndicator添加到您的第二個視圖控制器的viewDidLoad()方法中。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    self.startAnimating() //Add here 
    let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath as IndexPath)! 
    selectedCell.backgroundColor = UIColor.clear 
    selectedCell.tintColor = UIColor.clear 
    print("Chat Subject Has Benn Selected!") 
    let selectedItem = indexPath.row 
    print(selectedItem) 


    chatViewController.selectedChat = selectedItem 
    ///////////////////////Start Chat 
    print(EmailSignInViewController.id)  

    ////////////////////// Get chat Page 

    let urlPath3: String = "http://example/api?api_token=\(EmailSignInViewController.api_token)&id=\(EmailSignInViewController.id[selectedItem])" 


    print(urlPath3) 
    let url3: NSURL = NSURL(string: urlPath3)! 
    let request3: NSMutableURLRequest = NSMutableURLRequest(url: url3 as URL) 

    request3.httpMethod = "GET" 


    let queue3:OperationQueue = OperationQueue() 

    NSURLConnection.sendAsynchronousRequest(request3 as URLRequest, queue: OperationQueue.main) {(response, data, error) in 


     do { 
      let data = try Data(contentsOf: url3 as URL) 
      print(NSString(data: data, encoding: String.Encoding.utf8.rawValue)!) 

     } catch { 
      print(error.localizedDescription) 
     } 

    do { 

    if let jsonResult3 = try JSONSerialization.jsonObject(with: data!, options: []) as? Array<Any> { 

    for item in jsonResult3 { 
    print("item") 
    print(item) 

    if let dict = item as? NSDictionary { 
    if let type = dict.value(forKey: "type"){ 
    chatViewController.type.append(type as! Int) 

    print("type") 
    print("\([type])") 

     chattingViewController.checklist.append(type as! Int) 

    } 

    if let text = dict.value(forKey: "text"){ 

    chatViewController.text.append(text as!String) 


     chattingViewController.list.append(text as!String) 
    print("text") 
     print("\([text])") 


    } 
     if let created_at = dict.value(forKey: "created_at"){ 

       self.created.append(created_at as!String) 


      print("created_at") 
      print("\([created_at])") 
      print("\([created_at])") 


     } 



    } 
    } 

     print(jsonResult3) 

    print("ASynchronous\(jsonResult3)") 
     chatViewController.created_at = self.created 

     print("chat Page") 

    } 
    } catch let error as NSError { 
    print(error.localizedDescription) 
    } 
      self.activityIndicator.stopAnimating() //Removed here 
      self.performSegue(withIdentifier: "chatting", sender: nil) 

    } 

並從第二個視圖控制器中刪除這兩行。

chattingWaiting.startAnimating() 
chattingWaiting.isHidden = false 
+0

看起來像很多聊天應用程序,如viber或whatsapp或...我想當用戶選擇聊天兒子開始,因爲速度是非常重要的,之後,當Json完成UIActivityIndi​​catorView停止和隱藏 –

+0

這只是建議遵循最佳做法,但您可以通過上面添加的代碼來關閉activityIndi​​cator,但是如果您要將activityIndi​​cator添加到parentView,然後使用self.activityIndi​​cator.stopAnimating() –

+0

好吧,那麼我該如何添加它?只要將故事板拖到第一個視圖控制器代碼中? –

相關問題