如何在profileVC中使用登录响应值而不使用swift中的segue



我需要在配置文件视图控制器中使用登录响应值,为此我创建了如下登录响应模型

struct LoginModel: Codable {
let result:Result1?
let error:ErrorStatus?
}
struct ErrorStatus:Codable {
let message:String?
let meaning:String?
}
// MARK: - Result
struct Result1: Codable {
let userdata: Userdata
let token: String
}
// MARK: - Userdata
struct Userdata: Codable {
let id: Int
let fname, lname, email: String
let emailVcode, nickName: JSONNull?
let userType, isFeatured, emailVerifiedAt, password: String
let phoneCountryCode: JSONNull?
let phoneNo: String
}

我已经添加到LoginModel在LoginViewController如下

do{
let jsonModel = try JSONDecoder().decode(LoginModel.self, from: data)
print("new model (jsonModel.result)")

DispatchQueue.main.sync{
if jsonModel.error != nil{
}
else{
var nameLogin = jsonModel.result?.userdata.fname
var emailLogin = jsonModel.result?.userdata.email
print("login name (String(describing: nameLogin))")

let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController
self.navigationController?.pushViewController(vc!, animated: true)

}
}
}  

现在我需要总LoginModel值(fname,lname,email..和所有值)在ProfileViewcontroller,如何做到这一点,请帮助我在这里

您可以直接从LoginVC发送数据到profileVC:

do{

let jsonModel = try JSONDecoder().decode(LoginModel.self, from: data)
print("new model (jsonModel.result)")

DispatchQueue.main.sync{
if jsonModel.error != nil{
}
else{
var nameLogin = jsonModel.result?.userdata.fname
var emailLogin = jsonModel.result?.userdata.email
print("login name (String(describing: nameLogin))")
let storyboard =  UIStoryboard(name: "Main", bundle: Bundle.main)
let homeVc = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController
let profileVC = storyboard.instantiateViewController(identifier: "yourProfileVCIdentifier") as? ProfileViewController
profileVC?.userData = jsonModel.result?.userdata
self.navigationController?.pushViewController(homeVc!, animated: true)

}
}
}

那么你当然需要一个变量来保存数据在你的ProfileViewController

class ProfileViewController: UIViewController {

var userData:Userdata?
//other stuff
}

然而,如果你继续使用你的UserData,你应该,我认为你可以考虑保存这些数据在plist或UserDefaults

您可以简单地像这样创建:

class HomeViewController: UIViewController {
private var profile: LoginModel?

static func create(with profile: LoginModel?) -> HomeViewController? {
let controller = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController
controller?.profile = profile
return controller
}
}

或者只是声明一个内部属性:

class HomeViewController: UIViewController {
var profile: LoginModel?
}
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController
vc.profile = jsonModel
self.navigationController?.pushViewController(vc!, animated: true)

有很多解决方案

  1. 在HomeViewController中定义一个变量,你可以传递这样的数据:

    let homeVC = HomeViewController(nibName: "HomeViewController", bundle: nil)
    homeVC。data = userData

  2. 使用委托

  3. 使用UserDefaults
  4. 在LoginController:

UserDefaults.standard.set(userData, forKey: "user")

在HomeViewController:

let data = UserDefaults.standard.string(forKey: "user")

相关内容

  • 没有找到相关文章

最新更新