模型类在下一个视图控制器处为空

时间:2019-04-09 07:26:45

标签: ios swift

成功登录后,User的模型类为空。

UserViewController应该在加载中显示或打印user.name。但是当显示UserViewController时,模型类为空。为什么会这样?

import FBSDKLoginKit
import UIKit

//this the the viewcontrollr
class ViewController: UIViewController {
    var dict: [String: AnyObject]!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    @IBAction func loginButtonPressed(_: UIButton) {
        let fbLoginManager: FBSDKLoginManager = FBSDKLoginManager()
        fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { result, error in
            if error == nil {
                let fbloginresult: FBSDKLoginManagerLoginResult = result!
                if fbloginresult.grantedPermissions != nil {
                    if fbloginresult.grantedPermissions.contains("email") {
                        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                        let nextViewController: UIViewController = storyboard.instantiateViewController(withIdentifier: "userview") as! UserViewController

                        self.getFBUserData()

                        let appDel: AppDelegate = UIApplication.shared.delegate as! AppDelegate
                        appDel.window?.makeKeyAndVisible()
                        appDel.window?.rootViewController = nextViewController
                    }
                }
            }
        }
    }

    func getFBUserData() {
        if FBSDKAccessToken.current() != nil {
            FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (_, result, error) -> Void in
                if error == nil {
                    self.dict = (result as! [String: AnyObject])
                    let name = self.dict["name"]
                    User.name = name! as! String
                }
            })
        }
    }
}

当我转到UserViewController时:

import UIKit

class UserViewController: UIViewController {
    @IBOutlet var lblName: UILabel!
    @IBOutlet var lblEmail: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        print("your name is", User.name)
    }

    @IBAction func logOutButtonPressed(_: UIButton) {}
}

0 个答案:

没有答案