我正在使用SWRevealViewController作为选项卡式应用程序的FirstViewController上的滑出菜单。我正在从登录控制器实例化revealviewcontroller.revealview控制器连接到tabbarcontroller和tableview控制器,如下所示。
Image showing viewcontrollers in storyboard
问题:我正在尝试将用户名从logincontroller传递给FirstViewController,但我无法弄清楚如何。任何帮助表示赞赏
@IBAction func SignIn(_ sender:Any){
if emailField.text == "" || passwordField.text == "" {
message.text = "Please appropriate login credentials."
}
else {
FIRAuth.auth()?.createUser(withEmail: emailField.text!, password: passwordField.text!) { (user, error) in
if error == nil {
let DestinationVC = self.storyboard?.instantiateViewController(withIdentifier: "realview") as! SWRevealViewController
DestinationVC.loadView()
let firstVC = DestinationVC.frontViewController as! FirstViewController
self.present(DestinationVC, animated: true, completion: nil)
if let currentUser = FIRAuth.auth()?.currentUser {
firstVC.Displaylabel.text = currentUser.displayName
}
} else {
self.message.text = "\(error)"
}
}
}
}
答案 0 :(得分:0)
您的登录视图控制器应该是您的初始视图控制器。成功登录后,将revealviewcontroller类设置为rootview控制器。您可以将用户凭据另存为userdefault对象。据我所知,我已遵循此程序。
if let userName = NSUserDefaults.standardUserDefaults().objectForKey("userName") {
print("the name is ", (userName))
}
并在viewdidload中获取用户默认值
{{1}}
检查是否有效
答案 1 :(得分:0)
以下变更有效:
在登录视图控制器中,将用户ID保存到userdefaults
// aftersuccesfull login
如果let defaults:UserDefaults = UserDefaults.standard
{
let currentuser = FIRAuth.auth()?.currentUser
let userID = currentuser?.uid
defaults.set(userID, forKey: "userID")
defaults.synchronize()
}
在First View控制器中
//视图加载后
let defaults: UserDefaults = UserDefaults.standard
let user = defaults.string(forKey: "userID")
Displaylabel.text = user