2014-08-29 62 views
0

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

这是我的视图控制器:

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


} 

我需要显示在第二视图中的用户名,如果可能,他的自画像。我怎样才能做到这一点?

非常感谢你 问候 缺口

+0

有多种方法可以实现这一点。请参阅以下线程,其中包含解决您的问题的不同方法。所有在ObjectiveC中,但其中大多数也将在Swift中工作。 https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Apfelsaft 2014-08-29 06:33:29

回答

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

我知道这是客观-C,但所有的方法名是完全相同在Swift中也是一样。

相关问题