Shl*_*rtz 178 iphone facebook ios segue swift
我正在创建一个使用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???
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用上面的类方法来检查会话状态更改,它工作正常.
问:获得用户数据后,如何从此自定义类中转到下一个视图?
编辑:只是为了清楚,我在故事板上有一个带标识符的segue,我正试图找到一种方法来从一个不是视图控制器的类执行一个segue
Jér*_*ucq 329
如果故事板中存在segue,并且在两个视图之间有一个segue标识符,则可以使用以下命令以编程方式调用它:
performSegue(withIdentifier: "mySegueID", sender: nil)
Run Code Online (Sandbox Code Playgroud)
对于旧版本:
performSegueWithIdentifier("mySegueID", sender: nil)
Run Code Online (Sandbox Code Playgroud)
你也可以这样做:
presentViewController(nextViewController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
或者,如果您在导航控制器中:
self.navigationController?.pushViewController(nextViewController, animated: true)
Run Code Online (Sandbox Code Playgroud)
小智 20
您可以使用NSNotification
在自定义类中添加post方法:
NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)
Run Code Online (Sandbox Code Playgroud)
在ViewController中添加一个观察者:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "methodOFReceivedNotication:", name:"NotificationIdentifier", object: nil)
Run Code Online (Sandbox Code Playgroud)
在你的ViewController中添加功能:
func methodOFReceivedNotication(notification: NSNotification){
self.performSegueWithIdentifier("yourIdentifierInStoryboard", sender: self)
}
Run Code Online (Sandbox Code Playgroud)
Man*_*jan 15
如果故事板中存在segue,并且在两个视图之间有一个segue标识符,则可以使用以编程方式调用它
self.performSegueWithIdentifier("yourIdentifierInStoryboard", sender: self)
Run Code Online (Sandbox Code Playgroud)
如果您在导航控制器中
let viewController = YourViewController(nibName: "YourViewController", bundle: nil)
self.navigationController?.pushViewController(viewController, animated: true)
Run Code Online (Sandbox Code Playgroud)
我会建议您使用导航控制器进行第二种方法.
小智 14
您可以像这样使用segue:
self.performSegueWithIdentifier("push", sender: self)
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if segue.identifier == "push" {
}
}
Run Code Online (Sandbox Code Playgroud)
Tim*_*sen 12
Swift 3 - 也适用于SpriteKit
您可以使用NSNotification.
例:
1.)在故事板中创建一个segue并将标识符命名为"segue"
2.)在您正在进行的ViewController中创建一个函数.
func goToDifferentView() {
self.performSegue(withIdentifier: "segue", sender: self)
}
Run Code Online (Sandbox Code Playgroud)
3.)在ViewController的ViewDidLoad()中,您可以创建观察者.
NotificationCenter.default.addObserver(self, selector: #selector(goToDifferentView), name: "segue" as NSNotification.Name, object: nil)
Run Code Online (Sandbox Code Playgroud)
更新 - 上次我使用它时,我不得不将.addObserver
调用更改为以下代码以消除错误.
NotificationCenter.default.addObserver(self, selector: #selector(goToDifferentView), name: NSNotification.Name(rawValue: "segue"), object: nil)
Run Code Online (Sandbox Code Playgroud)
4.)在您正在查看的ViewController或Scene中,将Post方法添加到您希望触发segue的任何位置.
NotificationCenter.default.post(name: "segue" as NSNotification.Name, object: nil)
Run Code Online (Sandbox Code Playgroud)
更新 - 上次我使用它时,我不得不将.post
调用更改为以下代码以消除错误.
NotificationCenter.default.post(NSNotification(name: NSNotification.Name(rawValue: "segue"), object: nil) as Notification)
Run Code Online (Sandbox Code Playgroud)
小智 5
上面已经有很好的答案,我不想在执行继续之前把重点放在准备上。
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"]
}
Run Code Online (Sandbox Code Playgroud)
完成要传递到目标 VC 的数据后,请在适当的位置执行 Segue。您可以在 StoryBoard Identity 检查器的 StoryBoard ID 字段中设置“IdentifierOfDestinationVC”
performSegue(withIdentifier: "IdentifierOfDestinationVC", sender: nil)
Run Code Online (Sandbox Code Playgroud)