IOS - 如何使用swift以编程方式进行segue [英] IOS - How to segue programmatically using swift

查看:28
本文介绍了IOS - 如何使用swift以编程方式进行segue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个使用 Facebook SDK 来验证用户身份的应用.我正在尝试将 facebook 逻辑合并到一个单独的类中.这是代码(为简单起见已删除):

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: 
(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?

为了清楚起见,我在情节提要上有一个带有标识符的转场,我正在尝试找到一种方法来从不是视图控制器的类中执行转场

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

推荐答案

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

If your segue exists in the storyboard with a segue identifier between your two views, you can just call it programmatically using:

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

对于旧版本:

performSegueWithIdentifier("mySegueID", sender: nil)

你也可以这样做:

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

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

Or if you are in a Navigation controller:

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

这篇关于IOS - 如何使用swift以编程方式进行segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆