我正在创建一个使用Facebook SDK来验证用户身份的应用程序。我试图在一个单独的课上巩固facebook逻辑。下面是代码(为了简单起见,去掉了):

import Foundation

class FBManager {
    class func fbSessionStateChane(fbSession:FBSession!, fbSessionState:FBSessionState, error:NSError?){
        //... handling all session states
        FBRequestConnection.startForMeWithCompletionHandler { (conn: FBRequestConnection!, result: AnyObject!, error: NSError!) -> Void in
        
            println("Logged in user: \n\(result)");
        
            let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
            let loggedInView: UserViewController = storyboard.instantiateViewControllerWithIdentifier("loggedInView") as UserViewController
        
            loggedInView.result = result;
        
            //todo: segue to the next view???
        }
    }
}

我正在使用上面的类方法来检查会话状态的变化,它工作得很好。

问:一旦我有了用户的数据,我如何从这个自定义类中segue到下一个视图?

我在storyboard上有一个带标识符的segue,我试图找到一种方法从一个不是视图控制器的类执行segue


当前回答

上面已经有了很好的答案,我想把重点放在执行segue之前的准备上。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.destination is YourDestinationVC {
            let vc = segue.destination as? YourDestinationVC
            // "label" and "friends" are part of destinationVC
            vc?.label = "someText"
            vc?.friends = ["John","Mike","Garry"]
        }

一旦你完成了你想要传递给你的destinationVC的数据,然后在一个合适的地方执行你的segue。 你可以在StoryBoard ID字段的StoryBoard ID检查器中设置“IdentifierOfDestinationVC”

performSegue(withIdentifier: "IdentifierOfDestinationVC", sender: nil)

其他回答

你可以使用NSNotification

在自定义类中添加post方法:

NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)

在ViewController中添加一个观察者:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "methodOFReceivedNotication:", name:"NotificationIdentifier", object: nil)

在ViewController中添加函数:

func methodOFReceivedNotication(notification: NSNotification){
    self.performSegueWithIdentifier("yourIdentifierInStoryboard", sender: self)
}

这招对我很管用:

//Button method example
 @IBAction func LogOutPressed(_ sender: UIBarButtonItem) {

        do {
            try Auth.auth().signOut()
            navigationController?.popToRootViewController(animated: true)

        } catch let signOutError as NSError {
          print ("Error signing out: %@", signOutError)
        }


    }

如果你的segue存在于故事板中,并且在你的两个视图之间有一个segue标识符,你可以通过编程方式调用它:

performSegue(withIdentifier: "mySegueID", sender: nil)

对于旧版本:

performSegueWithIdentifier("mySegueID", sender: nil)

你还可以:

presentViewController(nextViewController, animated: true, completion: nil)

或者如果你在导航控制器中:

self.navigationController?.pushViewController(nextViewController, animated: true)

这对我很管用。

首先,在标识检查器中给storyboard中的视图控制器一个storyboard ID。然后使用下面的示例代码(确保类、故事板名称和故事板ID与您正在使用的匹配):

let viewController:
UIViewController = UIStoryboard(
    name: "Main", bundle: nil
).instantiateViewControllerWithIdentifier("ViewController") as UIViewController
// .instantiatViewControllerWithIdentifier() returns AnyObject!
// this must be downcast to utilize it

self.presentViewController(viewController, animated: false, completion: nil)

详情见http://sketchytech.blogspot.com/2012/11/instantiate-view-controller-using.html 最好的祝愿

上面已经有了很好的答案,我想把重点放在执行segue之前的准备上。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.destination is YourDestinationVC {
            let vc = segue.destination as? YourDestinationVC
            // "label" and "friends" are part of destinationVC
            vc?.label = "someText"
            vc?.friends = ["John","Mike","Garry"]
        }

一旦你完成了你想要传递给你的destinationVC的数据,然后在一个合适的地方执行你的segue。 你可以在StoryBoard ID字段的StoryBoard ID检查器中设置“IdentifierOfDestinationVC”

performSegue(withIdentifier: "IdentifierOfDestinationVC", sender: nil)