How to pass data to another controller on dismiss ViewController?












8














I want to pass my data from one ViewController to another VC on VC dismiss. Is it possible?



I've tried the next method and no success:



On button click:



self.dismiss(animated: true) { 
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "EditViewController") as! EditViewController
controller.segueArray = [values]
}


when EditViewController appears again, my segueArray is nil there.



How can I pass my data from my ViewController to the EditViewController on dismiss?










share|improve this question





























    8














    I want to pass my data from one ViewController to another VC on VC dismiss. Is it possible?



    I've tried the next method and no success:



    On button click:



    self.dismiss(animated: true) { 
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let controller = storyboard.instantiateViewController(withIdentifier: "EditViewController") as! EditViewController
    controller.segueArray = [values]
    }


    when EditViewController appears again, my segueArray is nil there.



    How can I pass my data from my ViewController to the EditViewController on dismiss?










    share|improve this question



























      8












      8








      8


      7





      I want to pass my data from one ViewController to another VC on VC dismiss. Is it possible?



      I've tried the next method and no success:



      On button click:



      self.dismiss(animated: true) { 
      let storyboard = UIStoryboard(name: "Main", bundle: nil)
      let controller = storyboard.instantiateViewController(withIdentifier: "EditViewController") as! EditViewController
      controller.segueArray = [values]
      }


      when EditViewController appears again, my segueArray is nil there.



      How can I pass my data from my ViewController to the EditViewController on dismiss?










      share|improve this question















      I want to pass my data from one ViewController to another VC on VC dismiss. Is it possible?



      I've tried the next method and no success:



      On button click:



      self.dismiss(animated: true) { 
      let storyboard = UIStoryboard(name: "Main", bundle: nil)
      let controller = storyboard.instantiateViewController(withIdentifier: "EditViewController") as! EditViewController
      controller.segueArray = [values]
      }


      when EditViewController appears again, my segueArray is nil there.



      How can I pass my data from my ViewController to the EditViewController on dismiss?







      ios swift uiviewcontroller storyboard uistoryboardsegue






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 30 '17 at 15:04









      Jack

      5,53232955




      5,53232955










      asked Apr 30 '17 at 13:00









      John Doe

      4813




      4813
























          3 Answers
          3






          active

          oldest

          votes


















          9














          The best way to pass data back to the previous view controller is through delegates... when going from ViewController A to B, pass view controller A as a delegate and on the viewWillDisappear method for ViewController B, call the delegate method in ViewController A.. Protocols would help define the delegate and the required methods to be implemented by previous VC. Here's a quick example:



          Protocol for passing data:



          protocol isAbleToReceiveData {
          func pass(data: String) //data: string is an example parameter
          }


          Viewcontroller A:



          class viewControllerA: UIViewController, isAbleToReceiveData {

          func pass(data: String) { //conforms to protocol
          // implement your own implementation
          }

          prepare(for: Segue) {
          /** code for passing data **/
          let vc2 = ViewCOntrollerB() /
          vc2.delegate = self //sets the delegate in the new viewcontroller
          //before displaying
          present(vc2)
          }
          }


          Dismissing viewcontroller:



          class viewControllerB: UIViewController {

          var delegate: isAbleToReceiveData

          viewWillDisappear {
          delegate.pass(data: "someData") //call the func in the previous vc
          }
          }





          share|improve this answer



















          • 1




            var delegate: isAbleToPassData this code throwing error . it should be var delegate: isAbleToPassData!
            – Nazmul Hasan
            Apr 30 '17 at 15:08












          • i'd suggest making it a optional value instead var delegate: isAbleToPassData? as it would reduce chances for crash
            – the_legend_27
            May 1 '17 at 11:23










          • and call the func using delegate?.pass(data: "SomeData")
            – the_legend_27
            May 1 '17 at 11:24










          • where does the protocol get written out in ViewController A or ViewController B?
            – Famic Tech
            Dec 19 '18 at 4:01



















          1














          In the dismiss completion block, you create a new instance of the EditViewController. I assume that another EditViewController instance exists back in the navigation stack, you need to find that instance & set the segueArray to values.
          That you can achieve by iterating through your navigation stack's viewcontrollers like:



          viewController.navigationController?.viewControllers.forEach({ (vc) in
          if let editVC = vc as? EditViewController {
          editVC.segueArray = ....
          }
          })


          But I would recommend to use the delegate pattern, like:



          protocol EditViewControllerDelegate: class {
          func setSegueArray(segues: [UIStoryboardSegue])
          }


          In the viewcontroller (call it just ViewController) where the dismiss block is, declare a delegate property:



          class ViewController: UIViewController {
          weak var delegate: EditViewControllerDelegate?
          ....
          }


          Then on presenting the instance of (I assume from EditViewController) ViewController set the delegate like:



          ...
          if let vc = presentingViewController as? ViewController {
          vc.delegate = self
          }


          And conform the EditViewController to the delegate protocol like:



          extension EditViewController: EditViewControllerDelegate {
          func setSegueArray(segues: [UIStoryboardSegue]) {
          // Do the data setting here eg. self.segues = segues
          }
          }





          share|improve this answer





























            0














            To detect when the back button is pressed on a view controller, I just use:



            override func didMove(toParentViewController parent: UIViewController?) {
            guard parent == nil else { return } // Back button pressed

            ... // Pass on the info as shown in you example
            } // didMoveToParentViewController





            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43706662%2fhow-to-pass-data-to-another-controller-on-dismiss-viewcontroller%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              9














              The best way to pass data back to the previous view controller is through delegates... when going from ViewController A to B, pass view controller A as a delegate and on the viewWillDisappear method for ViewController B, call the delegate method in ViewController A.. Protocols would help define the delegate and the required methods to be implemented by previous VC. Here's a quick example:



              Protocol for passing data:



              protocol isAbleToReceiveData {
              func pass(data: String) //data: string is an example parameter
              }


              Viewcontroller A:



              class viewControllerA: UIViewController, isAbleToReceiveData {

              func pass(data: String) { //conforms to protocol
              // implement your own implementation
              }

              prepare(for: Segue) {
              /** code for passing data **/
              let vc2 = ViewCOntrollerB() /
              vc2.delegate = self //sets the delegate in the new viewcontroller
              //before displaying
              present(vc2)
              }
              }


              Dismissing viewcontroller:



              class viewControllerB: UIViewController {

              var delegate: isAbleToReceiveData

              viewWillDisappear {
              delegate.pass(data: "someData") //call the func in the previous vc
              }
              }





              share|improve this answer



















              • 1




                var delegate: isAbleToPassData this code throwing error . it should be var delegate: isAbleToPassData!
                – Nazmul Hasan
                Apr 30 '17 at 15:08












              • i'd suggest making it a optional value instead var delegate: isAbleToPassData? as it would reduce chances for crash
                – the_legend_27
                May 1 '17 at 11:23










              • and call the func using delegate?.pass(data: "SomeData")
                – the_legend_27
                May 1 '17 at 11:24










              • where does the protocol get written out in ViewController A or ViewController B?
                – Famic Tech
                Dec 19 '18 at 4:01
















              9














              The best way to pass data back to the previous view controller is through delegates... when going from ViewController A to B, pass view controller A as a delegate and on the viewWillDisappear method for ViewController B, call the delegate method in ViewController A.. Protocols would help define the delegate and the required methods to be implemented by previous VC. Here's a quick example:



              Protocol for passing data:



              protocol isAbleToReceiveData {
              func pass(data: String) //data: string is an example parameter
              }


              Viewcontroller A:



              class viewControllerA: UIViewController, isAbleToReceiveData {

              func pass(data: String) { //conforms to protocol
              // implement your own implementation
              }

              prepare(for: Segue) {
              /** code for passing data **/
              let vc2 = ViewCOntrollerB() /
              vc2.delegate = self //sets the delegate in the new viewcontroller
              //before displaying
              present(vc2)
              }
              }


              Dismissing viewcontroller:



              class viewControllerB: UIViewController {

              var delegate: isAbleToReceiveData

              viewWillDisappear {
              delegate.pass(data: "someData") //call the func in the previous vc
              }
              }





              share|improve this answer



















              • 1




                var delegate: isAbleToPassData this code throwing error . it should be var delegate: isAbleToPassData!
                – Nazmul Hasan
                Apr 30 '17 at 15:08












              • i'd suggest making it a optional value instead var delegate: isAbleToPassData? as it would reduce chances for crash
                – the_legend_27
                May 1 '17 at 11:23










              • and call the func using delegate?.pass(data: "SomeData")
                – the_legend_27
                May 1 '17 at 11:24










              • where does the protocol get written out in ViewController A or ViewController B?
                – Famic Tech
                Dec 19 '18 at 4:01














              9












              9








              9






              The best way to pass data back to the previous view controller is through delegates... when going from ViewController A to B, pass view controller A as a delegate and on the viewWillDisappear method for ViewController B, call the delegate method in ViewController A.. Protocols would help define the delegate and the required methods to be implemented by previous VC. Here's a quick example:



              Protocol for passing data:



              protocol isAbleToReceiveData {
              func pass(data: String) //data: string is an example parameter
              }


              Viewcontroller A:



              class viewControllerA: UIViewController, isAbleToReceiveData {

              func pass(data: String) { //conforms to protocol
              // implement your own implementation
              }

              prepare(for: Segue) {
              /** code for passing data **/
              let vc2 = ViewCOntrollerB() /
              vc2.delegate = self //sets the delegate in the new viewcontroller
              //before displaying
              present(vc2)
              }
              }


              Dismissing viewcontroller:



              class viewControllerB: UIViewController {

              var delegate: isAbleToReceiveData

              viewWillDisappear {
              delegate.pass(data: "someData") //call the func in the previous vc
              }
              }





              share|improve this answer














              The best way to pass data back to the previous view controller is through delegates... when going from ViewController A to B, pass view controller A as a delegate and on the viewWillDisappear method for ViewController B, call the delegate method in ViewController A.. Protocols would help define the delegate and the required methods to be implemented by previous VC. Here's a quick example:



              Protocol for passing data:



              protocol isAbleToReceiveData {
              func pass(data: String) //data: string is an example parameter
              }


              Viewcontroller A:



              class viewControllerA: UIViewController, isAbleToReceiveData {

              func pass(data: String) { //conforms to protocol
              // implement your own implementation
              }

              prepare(for: Segue) {
              /** code for passing data **/
              let vc2 = ViewCOntrollerB() /
              vc2.delegate = self //sets the delegate in the new viewcontroller
              //before displaying
              present(vc2)
              }
              }


              Dismissing viewcontroller:



              class viewControllerB: UIViewController {

              var delegate: isAbleToReceiveData

              viewWillDisappear {
              delegate.pass(data: "someData") //call the func in the previous vc
              }
              }






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 19 '18 at 15:55









              Soropromo

              14616




              14616










              answered Apr 30 '17 at 13:14









              the_legend_27

              33119




              33119








              • 1




                var delegate: isAbleToPassData this code throwing error . it should be var delegate: isAbleToPassData!
                – Nazmul Hasan
                Apr 30 '17 at 15:08












              • i'd suggest making it a optional value instead var delegate: isAbleToPassData? as it would reduce chances for crash
                – the_legend_27
                May 1 '17 at 11:23










              • and call the func using delegate?.pass(data: "SomeData")
                – the_legend_27
                May 1 '17 at 11:24










              • where does the protocol get written out in ViewController A or ViewController B?
                – Famic Tech
                Dec 19 '18 at 4:01














              • 1




                var delegate: isAbleToPassData this code throwing error . it should be var delegate: isAbleToPassData!
                – Nazmul Hasan
                Apr 30 '17 at 15:08












              • i'd suggest making it a optional value instead var delegate: isAbleToPassData? as it would reduce chances for crash
                – the_legend_27
                May 1 '17 at 11:23










              • and call the func using delegate?.pass(data: "SomeData")
                – the_legend_27
                May 1 '17 at 11:24










              • where does the protocol get written out in ViewController A or ViewController B?
                – Famic Tech
                Dec 19 '18 at 4:01








              1




              1




              var delegate: isAbleToPassData this code throwing error . it should be var delegate: isAbleToPassData!
              – Nazmul Hasan
              Apr 30 '17 at 15:08






              var delegate: isAbleToPassData this code throwing error . it should be var delegate: isAbleToPassData!
              – Nazmul Hasan
              Apr 30 '17 at 15:08














              i'd suggest making it a optional value instead var delegate: isAbleToPassData? as it would reduce chances for crash
              – the_legend_27
              May 1 '17 at 11:23




              i'd suggest making it a optional value instead var delegate: isAbleToPassData? as it would reduce chances for crash
              – the_legend_27
              May 1 '17 at 11:23












              and call the func using delegate?.pass(data: "SomeData")
              – the_legend_27
              May 1 '17 at 11:24




              and call the func using delegate?.pass(data: "SomeData")
              – the_legend_27
              May 1 '17 at 11:24












              where does the protocol get written out in ViewController A or ViewController B?
              – Famic Tech
              Dec 19 '18 at 4:01




              where does the protocol get written out in ViewController A or ViewController B?
              – Famic Tech
              Dec 19 '18 at 4:01













              1














              In the dismiss completion block, you create a new instance of the EditViewController. I assume that another EditViewController instance exists back in the navigation stack, you need to find that instance & set the segueArray to values.
              That you can achieve by iterating through your navigation stack's viewcontrollers like:



              viewController.navigationController?.viewControllers.forEach({ (vc) in
              if let editVC = vc as? EditViewController {
              editVC.segueArray = ....
              }
              })


              But I would recommend to use the delegate pattern, like:



              protocol EditViewControllerDelegate: class {
              func setSegueArray(segues: [UIStoryboardSegue])
              }


              In the viewcontroller (call it just ViewController) where the dismiss block is, declare a delegate property:



              class ViewController: UIViewController {
              weak var delegate: EditViewControllerDelegate?
              ....
              }


              Then on presenting the instance of (I assume from EditViewController) ViewController set the delegate like:



              ...
              if let vc = presentingViewController as? ViewController {
              vc.delegate = self
              }


              And conform the EditViewController to the delegate protocol like:



              extension EditViewController: EditViewControllerDelegate {
              func setSegueArray(segues: [UIStoryboardSegue]) {
              // Do the data setting here eg. self.segues = segues
              }
              }





              share|improve this answer


























                1














                In the dismiss completion block, you create a new instance of the EditViewController. I assume that another EditViewController instance exists back in the navigation stack, you need to find that instance & set the segueArray to values.
                That you can achieve by iterating through your navigation stack's viewcontrollers like:



                viewController.navigationController?.viewControllers.forEach({ (vc) in
                if let editVC = vc as? EditViewController {
                editVC.segueArray = ....
                }
                })


                But I would recommend to use the delegate pattern, like:



                protocol EditViewControllerDelegate: class {
                func setSegueArray(segues: [UIStoryboardSegue])
                }


                In the viewcontroller (call it just ViewController) where the dismiss block is, declare a delegate property:



                class ViewController: UIViewController {
                weak var delegate: EditViewControllerDelegate?
                ....
                }


                Then on presenting the instance of (I assume from EditViewController) ViewController set the delegate like:



                ...
                if let vc = presentingViewController as? ViewController {
                vc.delegate = self
                }


                And conform the EditViewController to the delegate protocol like:



                extension EditViewController: EditViewControllerDelegate {
                func setSegueArray(segues: [UIStoryboardSegue]) {
                // Do the data setting here eg. self.segues = segues
                }
                }





                share|improve this answer
























                  1












                  1








                  1






                  In the dismiss completion block, you create a new instance of the EditViewController. I assume that another EditViewController instance exists back in the navigation stack, you need to find that instance & set the segueArray to values.
                  That you can achieve by iterating through your navigation stack's viewcontrollers like:



                  viewController.navigationController?.viewControllers.forEach({ (vc) in
                  if let editVC = vc as? EditViewController {
                  editVC.segueArray = ....
                  }
                  })


                  But I would recommend to use the delegate pattern, like:



                  protocol EditViewControllerDelegate: class {
                  func setSegueArray(segues: [UIStoryboardSegue])
                  }


                  In the viewcontroller (call it just ViewController) where the dismiss block is, declare a delegate property:



                  class ViewController: UIViewController {
                  weak var delegate: EditViewControllerDelegate?
                  ....
                  }


                  Then on presenting the instance of (I assume from EditViewController) ViewController set the delegate like:



                  ...
                  if let vc = presentingViewController as? ViewController {
                  vc.delegate = self
                  }


                  And conform the EditViewController to the delegate protocol like:



                  extension EditViewController: EditViewControllerDelegate {
                  func setSegueArray(segues: [UIStoryboardSegue]) {
                  // Do the data setting here eg. self.segues = segues
                  }
                  }





                  share|improve this answer












                  In the dismiss completion block, you create a new instance of the EditViewController. I assume that another EditViewController instance exists back in the navigation stack, you need to find that instance & set the segueArray to values.
                  That you can achieve by iterating through your navigation stack's viewcontrollers like:



                  viewController.navigationController?.viewControllers.forEach({ (vc) in
                  if let editVC = vc as? EditViewController {
                  editVC.segueArray = ....
                  }
                  })


                  But I would recommend to use the delegate pattern, like:



                  protocol EditViewControllerDelegate: class {
                  func setSegueArray(segues: [UIStoryboardSegue])
                  }


                  In the viewcontroller (call it just ViewController) where the dismiss block is, declare a delegate property:



                  class ViewController: UIViewController {
                  weak var delegate: EditViewControllerDelegate?
                  ....
                  }


                  Then on presenting the instance of (I assume from EditViewController) ViewController set the delegate like:



                  ...
                  if let vc = presentingViewController as? ViewController {
                  vc.delegate = self
                  }


                  And conform the EditViewController to the delegate protocol like:



                  extension EditViewController: EditViewControllerDelegate {
                  func setSegueArray(segues: [UIStoryboardSegue]) {
                  // Do the data setting here eg. self.segues = segues
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 30 '17 at 13:21









                  Bence Pattogato

                  1,2961021




                  1,2961021























                      0














                      To detect when the back button is pressed on a view controller, I just use:



                      override func didMove(toParentViewController parent: UIViewController?) {
                      guard parent == nil else { return } // Back button pressed

                      ... // Pass on the info as shown in you example
                      } // didMoveToParentViewController





                      share|improve this answer


























                        0














                        To detect when the back button is pressed on a view controller, I just use:



                        override func didMove(toParentViewController parent: UIViewController?) {
                        guard parent == nil else { return } // Back button pressed

                        ... // Pass on the info as shown in you example
                        } // didMoveToParentViewController





                        share|improve this answer
























                          0












                          0








                          0






                          To detect when the back button is pressed on a view controller, I just use:



                          override func didMove(toParentViewController parent: UIViewController?) {
                          guard parent == nil else { return } // Back button pressed

                          ... // Pass on the info as shown in you example
                          } // didMoveToParentViewController





                          share|improve this answer












                          To detect when the back button is pressed on a view controller, I just use:



                          override func didMove(toParentViewController parent: UIViewController?) {
                          guard parent == nil else { return } // Back button pressed

                          ... // Pass on the info as shown in you example
                          } // didMoveToParentViewController






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Apr 30 '17 at 13:52









                          KerCodex

                          657




                          657






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43706662%2fhow-to-pass-data-to-another-controller-on-dismiss-viewcontroller%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                              Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

                              A Topological Invariant for $pi_3(U(n))$