Passing Facebook user information between views. iOS Swift Xcode 6 [duplicate]

梦想的初衷 提交于 2019-12-08 09:51:02

问题


I am new in iOS app development and I'm having a hard time trying to pass facebook user information between views. My app has 2 views. I was able to implement facebook login using this tutorial because as you might know they still dont give oficial support to swift.

This is my view controller:

//  ViewController.swift


import UIKit

class ViewController: UIViewController, FBLoginViewDelegate{

    @IBOutlet var fbLoginView : FBLoginView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.fbLoginView.delegate = self
        self.fbLoginView.readPermissions = ["public_profile", "email", "user_friends"]
    }

    // FACEBOOK DELEGATE METHODS
    func loginViewShowingLoggedInUser(loginView : FBLoginView!) {
        println("User Logged In")
        println("This is where you perform a segue.")
        self.performSegueWithIdentifier("gotoMenu", sender: self)

    }

    func loginViewFetchedUserInfo(loginView : FBLoginView!, user: FBGraphUser){
        println(user.name)
    }

    func loginViewShowingLoggedOutUser(loginView : FBLoginView!) {
        println("User Logged Out")
    }

    func loginView(loginView : FBLoginView!, handleError:NSError) {
        println("Error: \(handleError.localizedDescription)")
    }

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


}

This is the second view controller:

//  SecondViewController.swift

import UIKit

class SecondViewController: UIViewController {

    @IBOutlet weak var greeting: UILabel!


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

    }

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


}

I need to show the user name in the second view and if possible his profile picture. How can I do this?

Thanks you very much Greeting nick


回答1:


In your LoginViewController simply override prepareForSegue: like so...

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"gotoMenu"]) {
         SecondViewController *vc = segue.destinationViewController;
         vc.username = self.username; // This is where you pass the data!!
    }
}

I know this is on objective-C but all the method names are exactly the same in Swift.



来源:https://stackoverflow.com/questions/25562467/passing-facebook-user-information-between-views-ios-swift-xcode-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!