Pass data between unrelated ViewControllers
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:
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
add a comment |
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:
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
add a comment |
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:
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
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:
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
ios swift delegates nsnotificationcenter
asked Nov 16 '18 at 2:29
Ricardo IsidroRicardo Isidro
6126
6126
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Nov 16 '18 at 20:37
drobersondroberson
251
251
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 16 '18 at 2:41
Sergio FloresSergio Flores
1126
1126
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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