Pass data between unrelated ViewControllers










1















I am trying to implement a login screen that would put data into a view controller, the login view controller is called by overriding tabBarController didSelect method, just as follows:



enter image description here



Note that there is not any segue between the TabBarController and the ThirdViewController, because I override tabBarController as follows:



import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate

// This delegate open the modal view after open the desired view.
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
if viewController is MyThirdViewController
if let loginPopupVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "LoginViewController")
tabBarController.present(loginPopupVC, animated: true)






Now, in my LoginViewController I parse a JSON in a struct (LoginResponse) with the data that is supposed to fill the ThirdViewController (name, surnames, sex, birth, and so on). I already do this with this snippet inside LoginViewController:



struct LoginResponse : Decodable 
var name: String
var surname: String
var sex: String
var birth: String


class LoginViewController: UIViewController, UITextFieldDelegate, XMLParserDelegate

@IBAction func cancelLogin(_ sender: UIButton)
//LoginViewController will close and ThirdViewController will open
dismiss(animated: true, completion: nil)


@IBAction func makeLogin(_ sender: UIButton)
//LoginViewController brings data, closing itself and opening ThirdViewController
self.updateUI()
self.dismiss(animated: true, completion: nil)


func updateUI()
do
let jsonDecoder = JSONDecoder()
let myjson = ""name": "MyName", "surname": "MySurname", "sex": "Male", "birth": "1980-05-15""
let loginResult = try jsonDecoder.decode(LoginResponse.self, from: Data(myjson.utf8))

catch let jsonErr
print(jsonErr)






Now, I want to pass that data (inside loginResult) to ThirdViewController.



I guess that I cannot invoke ThirdViewController from LoginViewController, because TabBarController already did it, and that it is necessary if I choose pass data using Delegate method or NotificationCenter method.



I wanna know which of the passing-data options between ViewControllers would work better in this case, because usually that methods are explained in examples with two view controllers connected by a segue, but in my case, the flow of the screens is unusual. Thanks.










share|improve this question


























    1















    I am trying to implement a login screen that would put data into a view controller, the login view controller is called by overriding tabBarController didSelect method, just as follows:



    enter image description here



    Note that there is not any segue between the TabBarController and the ThirdViewController, because I override tabBarController as follows:



    import UIKit

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate

    // This delegate open the modal view after open the desired view.
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
    if viewController is MyThirdViewController
    if let loginPopupVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "LoginViewController")
    tabBarController.present(loginPopupVC, animated: true)






    Now, in my LoginViewController I parse a JSON in a struct (LoginResponse) with the data that is supposed to fill the ThirdViewController (name, surnames, sex, birth, and so on). I already do this with this snippet inside LoginViewController:



    struct LoginResponse : Decodable 
    var name: String
    var surname: String
    var sex: String
    var birth: String


    class LoginViewController: UIViewController, UITextFieldDelegate, XMLParserDelegate

    @IBAction func cancelLogin(_ sender: UIButton)
    //LoginViewController will close and ThirdViewController will open
    dismiss(animated: true, completion: nil)


    @IBAction func makeLogin(_ sender: UIButton)
    //LoginViewController brings data, closing itself and opening ThirdViewController
    self.updateUI()
    self.dismiss(animated: true, completion: nil)


    func updateUI()
    do
    let jsonDecoder = JSONDecoder()
    let myjson = ""name": "MyName", "surname": "MySurname", "sex": "Male", "birth": "1980-05-15""
    let loginResult = try jsonDecoder.decode(LoginResponse.self, from: Data(myjson.utf8))

    catch let jsonErr
    print(jsonErr)






    Now, I want to pass that data (inside loginResult) to ThirdViewController.



    I guess that I cannot invoke ThirdViewController from LoginViewController, because TabBarController already did it, and that it is necessary if I choose pass data using Delegate method or NotificationCenter method.



    I wanna know which of the passing-data options between ViewControllers would work better in this case, because usually that methods are explained in examples with two view controllers connected by a segue, but in my case, the flow of the screens is unusual. Thanks.










    share|improve this question
























      1












      1








      1








      I am trying to implement a login screen that would put data into a view controller, the login view controller is called by overriding tabBarController didSelect method, just as follows:



      enter image description here



      Note that there is not any segue between the TabBarController and the ThirdViewController, because I override tabBarController as follows:



      import UIKit

      @UIApplicationMain
      class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate

      // This delegate open the modal view after open the desired view.
      func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
      if viewController is MyThirdViewController
      if let loginPopupVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "LoginViewController")
      tabBarController.present(loginPopupVC, animated: true)






      Now, in my LoginViewController I parse a JSON in a struct (LoginResponse) with the data that is supposed to fill the ThirdViewController (name, surnames, sex, birth, and so on). I already do this with this snippet inside LoginViewController:



      struct LoginResponse : Decodable 
      var name: String
      var surname: String
      var sex: String
      var birth: String


      class LoginViewController: UIViewController, UITextFieldDelegate, XMLParserDelegate

      @IBAction func cancelLogin(_ sender: UIButton)
      //LoginViewController will close and ThirdViewController will open
      dismiss(animated: true, completion: nil)


      @IBAction func makeLogin(_ sender: UIButton)
      //LoginViewController brings data, closing itself and opening ThirdViewController
      self.updateUI()
      self.dismiss(animated: true, completion: nil)


      func updateUI()
      do
      let jsonDecoder = JSONDecoder()
      let myjson = ""name": "MyName", "surname": "MySurname", "sex": "Male", "birth": "1980-05-15""
      let loginResult = try jsonDecoder.decode(LoginResponse.self, from: Data(myjson.utf8))

      catch let jsonErr
      print(jsonErr)






      Now, I want to pass that data (inside loginResult) to ThirdViewController.



      I guess that I cannot invoke ThirdViewController from LoginViewController, because TabBarController already did it, and that it is necessary if I choose pass data using Delegate method or NotificationCenter method.



      I wanna know which of the passing-data options between ViewControllers would work better in this case, because usually that methods are explained in examples with two view controllers connected by a segue, but in my case, the flow of the screens is unusual. Thanks.










      share|improve this question














      I am trying to implement a login screen that would put data into a view controller, the login view controller is called by overriding tabBarController didSelect method, just as follows:



      enter image description here



      Note that there is not any segue between the TabBarController and the ThirdViewController, because I override tabBarController as follows:



      import UIKit

      @UIApplicationMain
      class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate

      // This delegate open the modal view after open the desired view.
      func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
      if viewController is MyThirdViewController
      if let loginPopupVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "LoginViewController")
      tabBarController.present(loginPopupVC, animated: true)






      Now, in my LoginViewController I parse a JSON in a struct (LoginResponse) with the data that is supposed to fill the ThirdViewController (name, surnames, sex, birth, and so on). I already do this with this snippet inside LoginViewController:



      struct LoginResponse : Decodable 
      var name: String
      var surname: String
      var sex: String
      var birth: String


      class LoginViewController: UIViewController, UITextFieldDelegate, XMLParserDelegate

      @IBAction func cancelLogin(_ sender: UIButton)
      //LoginViewController will close and ThirdViewController will open
      dismiss(animated: true, completion: nil)


      @IBAction func makeLogin(_ sender: UIButton)
      //LoginViewController brings data, closing itself and opening ThirdViewController
      self.updateUI()
      self.dismiss(animated: true, completion: nil)


      func updateUI()
      do
      let jsonDecoder = JSONDecoder()
      let myjson = ""name": "MyName", "surname": "MySurname", "sex": "Male", "birth": "1980-05-15""
      let loginResult = try jsonDecoder.decode(LoginResponse.self, from: Data(myjson.utf8))

      catch let jsonErr
      print(jsonErr)






      Now, I want to pass that data (inside loginResult) to ThirdViewController.



      I guess that I cannot invoke ThirdViewController from LoginViewController, because TabBarController already did it, and that it is necessary if I choose pass data using Delegate method or NotificationCenter method.



      I wanna know which of the passing-data options between ViewControllers would work better in this case, because usually that methods are explained in examples with two view controllers connected by a segue, but in my case, the flow of the screens is unusual. Thanks.







      ios swift delegates nsnotificationcenter






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 2:29









      Ricardo IsidroRicardo Isidro

      6126




      6126






















          2 Answers
          2






          active

          oldest

          votes


















          0














          You can try something like this:



          import UIKit

          @UIApplicationMain
          class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate

          // This delegate open the modal view after open the desired view.
          func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
          if viewController is MyThirdViewController
          if let loginPopupVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "LoginViewController")
          loginPopupVC.delegate = tabBarController
          tabBarController.present(loginPopupVC, animated: true)






          For loginPopupVC:



          struct LoginResponse : Decodable 
          var name: String
          var surname: String
          var sex: String
          var birth: String


          class LoginViewController: UIViewController, UITextFieldDelegate, XMLParserDelegate

          var delegate: loginDelegate?

          @IBAction func cancelLogin(_ sender: UIButton)
          //LoginViewController will close and ThirdViewController will open
          dismiss(animated: true, completion: nil)


          @IBAction func makeLogin(_ sender: UIButton)
          //LoginViewController brings data, closing itself and opening ThirdViewController
          self.updateUI()
          self.dismiss(animated: true, completion: nil)


          func updateUI()
          do
          let jsonDecoder = JSONDecoder()
          let myjson = ""name": "MyName", "surname": "MySurname", "sex": "Male", "birth": "1980-05-15""
          let loginResult = try jsonDecoder.decode(LoginResponse.self, from: Data(myjson.utf8))
          // Pass data using delegate
          delegate?.handleLogin(with: loginResult)


          catch let jsonErr
          print(jsonErr)






          And then for your tabBarController class:



          protocol loginDelegate: class 
          func handleLogin(with object: LoginResponse)


          class myTabBarController: UITabBarController, loginDelegate

          // regular tabBarController lifecycle methods

          func handleLogin(with object: LoginResponse)
          // do work with data








          share|improve this answer






























            -1














            Right. I would suggest declaring a variable in loginPopupVC to contain a reference to ThirdViewController, so in tabBar:didSelect after instantiating the loginVC you pass it a reference to ThirdVC, but before presenting the login. In Login, said variable is class level, so you can access it from the updateUI func. Now, declare a method or variable in ThirdView to contain the json and just pass it.






            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%2f53330598%2fpass-data-between-unrelated-viewcontrollers%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              You can try something like this:



              import UIKit

              @UIApplicationMain
              class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate

              // This delegate open the modal view after open the desired view.
              func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
              if viewController is MyThirdViewController
              if let loginPopupVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "LoginViewController")
              loginPopupVC.delegate = tabBarController
              tabBarController.present(loginPopupVC, animated: true)






              For loginPopupVC:



              struct LoginResponse : Decodable 
              var name: String
              var surname: String
              var sex: String
              var birth: String


              class LoginViewController: UIViewController, UITextFieldDelegate, XMLParserDelegate

              var delegate: loginDelegate?

              @IBAction func cancelLogin(_ sender: UIButton)
              //LoginViewController will close and ThirdViewController will open
              dismiss(animated: true, completion: nil)


              @IBAction func makeLogin(_ sender: UIButton)
              //LoginViewController brings data, closing itself and opening ThirdViewController
              self.updateUI()
              self.dismiss(animated: true, completion: nil)


              func updateUI()
              do
              let jsonDecoder = JSONDecoder()
              let myjson = ""name": "MyName", "surname": "MySurname", "sex": "Male", "birth": "1980-05-15""
              let loginResult = try jsonDecoder.decode(LoginResponse.self, from: Data(myjson.utf8))
              // Pass data using delegate
              delegate?.handleLogin(with: loginResult)


              catch let jsonErr
              print(jsonErr)






              And then for your tabBarController class:



              protocol loginDelegate: class 
              func handleLogin(with object: LoginResponse)


              class myTabBarController: UITabBarController, loginDelegate

              // regular tabBarController lifecycle methods

              func handleLogin(with object: LoginResponse)
              // do work with data








              share|improve this answer



























                0














                You can try something like this:



                import UIKit

                @UIApplicationMain
                class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate

                // This delegate open the modal view after open the desired view.
                func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
                if viewController is MyThirdViewController
                if let loginPopupVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "LoginViewController")
                loginPopupVC.delegate = tabBarController
                tabBarController.present(loginPopupVC, animated: true)






                For loginPopupVC:



                struct LoginResponse : Decodable 
                var name: String
                var surname: String
                var sex: String
                var birth: String


                class LoginViewController: UIViewController, UITextFieldDelegate, XMLParserDelegate

                var delegate: loginDelegate?

                @IBAction func cancelLogin(_ sender: UIButton)
                //LoginViewController will close and ThirdViewController will open
                dismiss(animated: true, completion: nil)


                @IBAction func makeLogin(_ sender: UIButton)
                //LoginViewController brings data, closing itself and opening ThirdViewController
                self.updateUI()
                self.dismiss(animated: true, completion: nil)


                func updateUI()
                do
                let jsonDecoder = JSONDecoder()
                let myjson = ""name": "MyName", "surname": "MySurname", "sex": "Male", "birth": "1980-05-15""
                let loginResult = try jsonDecoder.decode(LoginResponse.self, from: Data(myjson.utf8))
                // Pass data using delegate
                delegate?.handleLogin(with: loginResult)


                catch let jsonErr
                print(jsonErr)






                And then for your tabBarController class:



                protocol loginDelegate: class 
                func handleLogin(with object: LoginResponse)


                class myTabBarController: UITabBarController, loginDelegate

                // regular tabBarController lifecycle methods

                func handleLogin(with object: LoginResponse)
                // do work with data








                share|improve this answer

























                  0












                  0








                  0







                  You can try something like this:



                  import UIKit

                  @UIApplicationMain
                  class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate

                  // This delegate open the modal view after open the desired view.
                  func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
                  if viewController is MyThirdViewController
                  if let loginPopupVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "LoginViewController")
                  loginPopupVC.delegate = tabBarController
                  tabBarController.present(loginPopupVC, animated: true)






                  For loginPopupVC:



                  struct LoginResponse : Decodable 
                  var name: String
                  var surname: String
                  var sex: String
                  var birth: String


                  class LoginViewController: UIViewController, UITextFieldDelegate, XMLParserDelegate

                  var delegate: loginDelegate?

                  @IBAction func cancelLogin(_ sender: UIButton)
                  //LoginViewController will close and ThirdViewController will open
                  dismiss(animated: true, completion: nil)


                  @IBAction func makeLogin(_ sender: UIButton)
                  //LoginViewController brings data, closing itself and opening ThirdViewController
                  self.updateUI()
                  self.dismiss(animated: true, completion: nil)


                  func updateUI()
                  do
                  let jsonDecoder = JSONDecoder()
                  let myjson = ""name": "MyName", "surname": "MySurname", "sex": "Male", "birth": "1980-05-15""
                  let loginResult = try jsonDecoder.decode(LoginResponse.self, from: Data(myjson.utf8))
                  // Pass data using delegate
                  delegate?.handleLogin(with: loginResult)


                  catch let jsonErr
                  print(jsonErr)






                  And then for your tabBarController class:



                  protocol loginDelegate: class 
                  func handleLogin(with object: LoginResponse)


                  class myTabBarController: UITabBarController, loginDelegate

                  // regular tabBarController lifecycle methods

                  func handleLogin(with object: LoginResponse)
                  // do work with data








                  share|improve this answer













                  You can try something like this:



                  import UIKit

                  @UIApplicationMain
                  class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate

                  // This delegate open the modal view after open the desired view.
                  func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
                  if viewController is MyThirdViewController
                  if let loginPopupVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "LoginViewController")
                  loginPopupVC.delegate = tabBarController
                  tabBarController.present(loginPopupVC, animated: true)






                  For loginPopupVC:



                  struct LoginResponse : Decodable 
                  var name: String
                  var surname: String
                  var sex: String
                  var birth: String


                  class LoginViewController: UIViewController, UITextFieldDelegate, XMLParserDelegate

                  var delegate: loginDelegate?

                  @IBAction func cancelLogin(_ sender: UIButton)
                  //LoginViewController will close and ThirdViewController will open
                  dismiss(animated: true, completion: nil)


                  @IBAction func makeLogin(_ sender: UIButton)
                  //LoginViewController brings data, closing itself and opening ThirdViewController
                  self.updateUI()
                  self.dismiss(animated: true, completion: nil)


                  func updateUI()
                  do
                  let jsonDecoder = JSONDecoder()
                  let myjson = ""name": "MyName", "surname": "MySurname", "sex": "Male", "birth": "1980-05-15""
                  let loginResult = try jsonDecoder.decode(LoginResponse.self, from: Data(myjson.utf8))
                  // Pass data using delegate
                  delegate?.handleLogin(with: loginResult)


                  catch let jsonErr
                  print(jsonErr)






                  And then for your tabBarController class:



                  protocol loginDelegate: class 
                  func handleLogin(with object: LoginResponse)


                  class myTabBarController: UITabBarController, loginDelegate

                  // regular tabBarController lifecycle methods

                  func handleLogin(with object: LoginResponse)
                  // do work with data









                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 20:37









                  drobersondroberson

                  251




                  251























                      -1














                      Right. I would suggest declaring a variable in loginPopupVC to contain a reference to ThirdViewController, so in tabBar:didSelect after instantiating the loginVC you pass it a reference to ThirdVC, but before presenting the login. In Login, said variable is class level, so you can access it from the updateUI func. Now, declare a method or variable in ThirdView to contain the json and just pass it.






                      share|improve this answer



























                        -1














                        Right. I would suggest declaring a variable in loginPopupVC to contain a reference to ThirdViewController, so in tabBar:didSelect after instantiating the loginVC you pass it a reference to ThirdVC, but before presenting the login. In Login, said variable is class level, so you can access it from the updateUI func. Now, declare a method or variable in ThirdView to contain the json and just pass it.






                        share|improve this answer

























                          -1












                          -1








                          -1







                          Right. I would suggest declaring a variable in loginPopupVC to contain a reference to ThirdViewController, so in tabBar:didSelect after instantiating the loginVC you pass it a reference to ThirdVC, but before presenting the login. In Login, said variable is class level, so you can access it from the updateUI func. Now, declare a method or variable in ThirdView to contain the json and just pass it.






                          share|improve this answer













                          Right. I would suggest declaring a variable in loginPopupVC to contain a reference to ThirdViewController, so in tabBar:didSelect after instantiating the loginVC you pass it a reference to ThirdVC, but before presenting the login. In Login, said variable is class level, so you can access it from the updateUI func. Now, declare a method or variable in ThirdView to contain the json and just pass it.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 16 '18 at 2:41









                          Sergio FloresSergio Flores

                          1126




                          1126



























                              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.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330598%2fpass-data-between-unrelated-viewcontrollers%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

                              Top Tejano songwriter Luis Silva dead of heart attack at 64

                              政党

                              天津地下鉄3号線