0

I am trying to pop a ViewController from a navigationController and send data using a segue programmatically. I cannot seem to successfully transfer the data across to my destination viewController. Below is my code. Please can someone advise on where I am going wrong?

I am trying to get the variable 'segueContainerNewFolderCreatedBool' in the destination viewController to be assigned the value of 'true'. But When the desination viewController loads, in the viewWillAppear() the value of 'segueContainerNewFolderCreatedBool' is always 'false'. I am out of ideas.

Destination viewController:

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

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

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

   }     ...
 }

Source 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()
jamesMcKey
  • 481
  • 5
  • 28
  • Why are you using `popViewController` to navigate to a new view controller? Looks to me like you're not able to set that bool because you're never actually adding the destination view controller to the view stack. – brandonscript Oct 24 '17 at 18:08
  • Also, please don't start your class names with a lowercase letter. Use uppercase. – brandonscript Oct 24 '17 at 18:09

2 Answers2

0

try this one.

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()

Please see below link as well.

How to identify previous view controller in navigation stack

Yongjoon
  • 104
  • 1
  • 4
  • Yes i am trying to go back, but need to remove the present viewController from the stack. Isn't your answer what i have done above? popping the current VC from the stack? I am able to pop the current view controller but fail in transferring the data to the destination vc – jamesMcKey Oct 24 '17 at 21:19
0

You're not trying to pop, you're trying to present a UIViewController. Also, the easiest way to use segue is to create it in Storyboard and then just call it from viewController1:

performSegue(withIdentifier: YOUR_SEGUE_IDENTIFIER, sender: self)

In order to transfer data using segue you need to use prepare(for segue: UIStoryboardSegue, sender: Any?). Here is an example:

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
        }
    }
}
Đorđe Nilović
  • 3,600
  • 1
  • 25
  • 20