通过类函数从另一个Swift文件执行Segue [英] Perform Segue From Another Swift File via a class function

查看:71
本文介绍了通过类函数从另一个Swift文件执行Segue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果所保存的类未连接到viewController,如何使用实现的类函数执行segue?

How can I use a class function I implement to perform a segue if the class it is held in is not connected to a viewController?

推荐答案

要执行segue,您需要segue源自的视图控制器实例-例如:

In order to perform a segue, you need the instance of view controller where the segue originates from - for example:

class MyViewController {
}

class MyClass {
    showNextView(fromViewController: UIViewController) {
        fromViewController.performSegueWithIdentifier("segue_id", sender: fromViewController)
    }
}

但是,我不鼓励这种互动.不要告诉视图控制器如何显示视图-让它自己做,然后问吧.

However I discourage this kind of interaction. Don't tell the view controller how to show a view - let it do on its own, and just ask it.

因此,我建议在视图控制器中创建一个实例方法:

So, I suggest creating an instance method in your view controller:

class MyViewController {
    ...
    func goToNextView() {
       performSegueWithIdentifier("segue_id", sender: self)
    }
}

并使用它进行过渡:

class MyClass {
    showNextView(fromViewController: MyViewController) {
        fromViewController.goToNextView()
    }
}

这篇关于通过类函数从另一个Swift文件执行Segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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