I'm creating an app that uses the Facebook SDK to authenticate users. I'm trying to consolidate the facebook logic in a separate class. Here is the code (stripped for simplicity):
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???
}
}
}
I'm using the above class method to check session state changes, and it works fine.
Q: Once I have the user's data, how can I segue to the next view from within this custom class?
EDIT: just to be clear, I have a segue with identifier on the storyboard, and I'm trying to find a way to perform a segue from a class which is not the view controller
Swift 3 - Also works with SpriteKit
You can use NSNotification.
Example:
1.) Create a segue in the storyboard and name the identifier "segue"
2.) Create a function in the ViewController you are segueing from.
3.) In the ViewDidLoad() of your ViewController you are segueing from create the observer.
Update - Last time I used this I had to change the
.addObserver
call to the following code to silence the errors.4.) In the ViewController or Scene you are segueing to, add the Post Method wherever you want the segue to be triggered.
Update - Last time I used this I had to change the
.post
call to the following code to silence the errors.If your segue exists in the storyboard with a segue identifier between your two views, you can just call it programmatically using:
For older versions:
You could also do:
Or if you are in a Navigation controller:
If your segue exists in the storyboard with a segue identifier between your two views, you can just call it programmatically using
If you are in Navigation controller
I will recommend you for second approach using navigation controller.
You can use segue like this:
This worked for me.
First of all give the view controller in your storyboard a Storyboard ID inside the identity inspector. Then use the following example code (ensuring the class, storyboard name and story board ID match those that you are using):
For more details see http://sketchytech.blogspot.com/2012/11/instantiate-view-controller-using.html best wishes
You can use NSNotification
Add a post method in your custom class:
Add an observer in your ViewController:
Add function in you ViewController: