As long as I can understand your question. Once the signup is completed, you want to access that user
information on the home screen( or maybe at several other places). You can achieve it in many ways, Below are two of them.
Just to make this answer more explanatory, consider SignUP screen is represented by SignUpViewController
and Home screen is being represented by HomeViewController
By passing the value user
You can pass it once the signup is completed. I am sure once the sign up is completed. You must be initializing the HomeViewController
somewhere in the SignUp screen and pushing
or presenting
the HomeViewController
.
Define a property (let sayuser
) type of User
in HomeViewController
When you initiale the HomeViewController
from the signup screen, You just need to pass it before you push or present the HomeViewController
.
Example code
// Your user struct
struct User {
let email: String
let name: String
}
class SignUpViewController: UIViewController {
//This is just to show you, I am sure you must be initializing the user appropriately.
let user = User(email: "xyz@gmail.com", name: "Paul")
//Assuming you have a function which is called as soon signup is completed
func didSignUpCompleted() {
// initialise the HomeViewController, here I am assuming you have storyboard corresponding to HomeViewController
let storyboard = UIStoryboard(name: "youstoryboarname", bundle: nil)
let homeViewController = storyboard.instantiateViewController(withIdentifier: "homeViewControllerIdentifier") as! HomeViewController
// This is the point where you have to pass that user property to HomeViewController
homeViewController.user = user
// If the viewcontroller is embeded in UINavigationController
self.navigationController?.pushViewController(homeViewController, animated: true)
}
}
class HomeViewController: UIViewController {
// this will be set from SignUp screen
var user: User?
func viewDidLoad() {
super.viewDidLoad()
guard let signedUpUser = user else { return }
let welcomeMessgae = "Welcome, \(signedUpUser.name)"
}
}
Singleton Design Pattern
If you have a use case where you may be needed this user
property at several places then you can create a singleton class that will hold this user
information. Define the user
property in that singleton class and set it when you pushing
or presenting
HomeViewController
from SignUpViewController
.
Let's assume that AppSession
is the singleton class which holds the user
property
class AppSession {
static let current = AppSession()
// user property. which will be set from sign up screen
var user: User?
private init() { }
}
Let's assume below function is called once sign-up is completed (defined somewhere in SignUpViewController
)
func didSignUpCompleted() {
// set the `user`
AppSession.current.user = User(email: "xyz@gmail.com", name: "Paul")
}
Later on, you can access it from anywhere like this
class HomeViewController: UIViewController {
func viewDidLoad() {
super.viewDidLoad()
guard let signedUpUser = AppSession.current.user else { return }
let welcomeMessgae = "Welcome, \(signedUpUser.name)"
}
}
I hope it may give you a bit of understanding so that you can implement either of the mentioned approaches as per your use case.