在视图之间传递Facebook用户信息。 iOS Swift Xcode 6

时间:2014-08-29 06:01:14

标签: ios facebook swift segue

我是iOS应用程序开发的新手,我很难在视图之间传递facebook用户信息。我的应用有2个视图。我能够使用this tutorial实现facebook登录,因为你可能知道他们仍然没有给swift提供官方支持。

这是我的视图控制器:

//  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.
    }


}

这是第二个视图控制器:

//  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.
    }


}

我需要在第二个视图中显示用户名,如果可能,还需要显示他的个人资料图片。我怎么能这样做?

非常感谢你 欢迎 缺口

1 个答案:

答案 0 :(得分:0)

在你的LoginViewController中简单地覆盖prepareForSegue:就像这样......

// 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!!
    }
}

我知道这是在Objective-C上,但所有方法名称在Swift中完全相同。