无法以编程方式使用segue传输数据 - swift3

时间:2017-10-24 18:02:03

标签: ios swift segue

我试图从navigationController弹出一个ViewController,并以编程方式使用segue发送数据。我似乎无法成功地将数据传输到我的目标viewController。以下是我的代码。请问有人可以告诉我哪里出错了吗?

我正在尝试获取变量' segueContainerNewFolderCreatedBool'在目标viewController中被赋值为' true'。但是当desination viewController加载时,在viewWillAppear()中的值为' segueContainerNewFolderCreatedBool'总是“假”#39;我没有想法。

目标viewController:

 class menuTableViewController: UITableViewController{
 ...
   var segueContainerNewFolderCreatedBool = false

   override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(true)

      print("self.segueContainerNewFolderCreatedBool : \(self.segueContainerNewFolderCreatedBool)")

   }     ...
 }

源viewController:

  let task = session.dataTask(with: request, completionHandler: { (data, response, error) in

            if let jsonData = data{

                do{
                    let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: [])

                    guard
                        let jsonDictionary = jsonObject as? [String:String],
                        let message = jsonDictionary["msg"] else{
                            print("unexpected data structure from server")
                            return
                    }

                    if(message == "folder created success"){
                        // Folder created success
                        print("folder created success")


                        // Generate segue to tansfer data - let 'menuTableViewController' know that new
                        // folder has been created - to update tableView of user folders
                        let storyBoard: UIStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
                        let destinationViewController = storyBoard.instantiateViewController(withIdentifier: "landingPage") as! menuTableViewController
                        let sourceViewController = storyBoard.instantiateViewController(withIdentifier: "createUserFolder") as! CreateNewUserFolderPubMed

                        let segue = UIStoryboardSegue.init(identifier: "nFolderSegue", source: sourceViewController, destination: destinationViewController, performHandler: {

                            DispatchQueue.main.async{

                                destinationViewController.segueContainerNewFolderCreatedBool = true

                                if let navController = self.navigationController{
                                    navController.popViewController(animated: true)
                                }
                            }// End DispatchQueue

                        })
                        segue.perform()


                    }else{
                        // Folder created fail
                    }
                }catch let error{
                    print("error detail: \(error)")
                }
            }else if let requestError = error{
                print("print error: \(requestError)")
            }else{
                print("unexpected error")
            }
        })
        task.resume()

2 个答案:

答案 0 :(得分:0)

尝试这个。

let task = session.dataTask(with: request, completionHandler: { (data, response, error) in

            if let jsonData = data{

                do{
                    let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: [])

                    guard
                        let jsonDictionary = jsonObject as? [String:String],
                        let message = jsonDictionary["msg"] else{
                            print("unexpected data structure from server")
                            return
                    }

                    if(message == "folder created success"){
                        // Folder created success
                        print("folder created success")


                            DispatchQueue.main.async{

                                 for element in (self.navigationController?.viewControllers)! {
                                      if element.isKind(of:menuTableViewController) {
                                       let prevVC  = element as? menuTableViewController
                                       prevVC?. segueContainerNewFolderCreatedBool = true
                                        self.navigationController?.popViewController(animated: true)
                                      break
            }
        }


                            }// End DispatchQueue

                        })
                        segue.perform()


                    }else{
                        // Folder created fail
                    }
                }catch let error{
                    print("error detail: \(error)")
                }
            }else if let requestError = error{
                print("print error: \(requestError)")
            }else{
                print("unexpected error")
            }
        })
        task.resume()

请参阅以下链接。

How to identify previous view controller in navigation stack

答案 1 :(得分:0)

您并未尝试弹出,而是尝试呈现 UIViewController。此外,使用segue最简单的方法是在Storyboard中创建它,然后从viewController1中调用它:

performSegue(withIdentifier: YOUR_SEGUE_IDENTIFIER, sender: self)

要使用segue传输数据,您需要使用prepare(for segue: UIStoryboardSegue, sender: Any?)。这是一个例子:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "YOUR_SEGUE_IDENTIFIER" {
        if let destination = segue.destination as? YOUR_VC_CLASS {
            destination.YOUR_VARIABLE = YOUR_VALUE
        }
    }
}