2015-10-20 52 views
0

似乎并没有将我的数据传递给新的视图控制器。 事实上,它只能在第一个视图控制器中工作,一旦登录。 我相信我的工作中有一些错误。 请告诉我我错了,我是初学者,所以请精确和高效。非常感谢。Facebook SDK swift:提取用户数据的问题(segue)

class ViewController: UIViewController, FBSDKLoginButtonDelegate 
{ 

    override func viewDidLoad() 
    { 
     super.viewDidLoad() 


     if FBSDKAccessToken.currentAccessToken() != nil { 
      //user already has access token 
      self.logUserData() 
     } else { 

     loginButton.readPermissions = ["public_profile", "email", "user_friends"] 

     loginButton.delegate = self 


     self.Photo.image = UIImage(named: "Why subtle bckd image1") 
     self.Photo2.image = UIImage(named: "Un combo unique pict") 

    } 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 




    // MARK: PR/VAR 


    var firstName: String! 
    var lastName: String! 
    var email: String! 



    @IBOutlet var loginButton: FBSDKLoginButton! 

    @IBAction func loginButtonsend(sender: UIButton) { 

     sender.setTitle("firstName", forState: UIControlState.Normal) 
     sender.setTitle("lastName", forState: UIControlState.Normal) 
     sender.setTitle("email", forState: UIControlState.Normal) 
    } 



    @IBOutlet var Photo: UIImageView! 
    @IBOutlet var Photo2: UIImageView! 






    // MARK: FACEBOOK LOGIN 


    override func viewWillAppear(animated: Bool) { 
     self.logUserData() 

    } 


    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) 
    { if error == nil { 

     let fbAccessToken = FBSDKAccessToken.currentAccessToken().tokenString 
      println("Logged in") 

    } else { println(error.localizedDescription) } } 



    func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) 
    { 
     println("User logged out.") 

    } 

    func logUserData() 
      { 
       let graphRequest = FBSDKGraphRequest(graphPath: "me?fields=id,email,first_name,last_name", parameters: ["fields": "first_name,email,last_name"]) 
       graphRequest.startWithCompletionHandler { (connection, result, error) -> Void in 

        if error != nil 
        { 
         // Process error 
         println(error) 
        } 
        else 
        { 
         println(result.grantedPermissions) 
         println("fetched user = \(result)") 
         var firstName = result.valueForKey("first_name") 
         println("firstName = \(firstName)") 
         var lastName = result.valueForKey("last_name") 
         println("lastName is = \(lastName)") 
         var email = result.valueForKey("email") 
         println("email is = \(email)") 

         self.performSegueWithIdentifier("showNew", sender: self) 
        } 

       } 

    } 
       // SEGUE: 

       override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

        if (segue.identifier == "showNew") { 

         if let destinationVC = segue.destinationViewController as? NewViewController { 
          destinationVC.firstName = firstName 
          destinationVC.lastName = lastName 
          destinationVC.email = email 

        } 
       } 
    } 

}

在我新的视图控制器我有以下几点:

class NewViewController: UIViewController { 

var firstName: String! 
var lastName: String! 
var email: String! 


@IBOutlet var firstNameLabel: UILabel! 
@IBOutlet var lastNameLabel: UILabel! 
@IBOutlet var emailLabel: UILabel! 


override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 

    if (firstName != nil) { 
     firstNameLabel.text = firstName 
    } 

    if (lastName != nil) { 
     lastNameLabel.text = lastName 
    } 

    if (email != nil) { 
     emailLabel.text = email 

    } 

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 





@IBAction func logoutButtonTapped(sender: AnyObject) { 





    let loginManager = FBSDKLoginManager() 

    loginManager.logOut() 





    let loginPage = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController") as ViewController 



    //? 

    let loginPageNav = UINavigationController (rootViewController: loginPage) 





    let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate 



    appDelegate.window?.rootViewController = loginPageNav 



} 



/* 
// MARK: - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { 
    // Get the new view controller using segue.destinationViewController. 
    // Pass the selected object to the new view controller. 
} 
*/ 

}

回答

0

好像你从来没有设置您的ViewController的变量,但而是创建三个与您应该在logUserData上使用的名称相同的名称。只需更改为:

println(result.grantedPermissions) 
println("fetched user = \(result)") 
firstName = result.valueForKey("first_name") 
println("firstName = \(firstName)") 
lastName = result.valueForKey("last_name") 
println("lastName is = \(lastName)") 
email = result.valueForKey("email") 
println("email is = \(email)") 
+0

谢谢! –