以编程方式从UIView执行Segue [英] Performing a Segue from a UIView programmatically

查看:51
本文介绍了以编程方式从UIView执行Segue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个自定义UIView,当点击该UIView时,它会锁定到新的视图控制器。我想以编程方式而不是在Interface Builder中执行此操作。执行此操作时如何获得对目标视图控制器的引用?我想我不只是创建一个视图并将其设置为下一个视图或其他视图,所以引用来自何处?由于在情节提要中完成此操作后,您只需选择要将segue作为目的地的位置即可,因此这不是问题,但是从编程上来说,我不知道引用的来源。

I'm trying to create a custom UIView that will, when tapped, segue to a new view controller. I'd like to do this programmatically rather than in Interface Builder. How do I get a reference to the destination view controller when I'm doing this? I assume I don't just create one and set it as the next view or something, so where does the reference come from? Since when it's done in the storyboard you simply choose where you want the segue to have as the destination, it's not an issue there, however programmatically I don't understand where that reference comes from.

我也在Swift中这样做。我不认为这确实有很大的区别,但是可能会有所改变。

I'm doing this in Swift as well. I don't think that really makes a big difference, but it might change something.

我尝试使用添加了tapGestureRecognizer的UIView,然后在点击时执行segue ,但是我遇到了无法识别的选择器错误。

I tried using a UIView with a tapGestureRecognizer added, which then performed the segue when tapped, however I was getting an unrecognized selector error.

推荐答案

如果在情节提要中声明了UIViewController,则必须设置其情节提要ID,然后,除了执行segue之外,还可以使用类似的方法(没有运行它,但是在obj-c中执行了完全相同的操作,因此我认为它会做得很好):

If you declared your UIViewController in storyboard you have to set its Storyboard Id, and then, instead of performing segue you can use something like this (didn't run it, but done exact the same thing in obj-c, so I think it will do well):

let storyboard : UIStoryboard = UIStoryboard(name: "myStoryboardName", bundle: nil);
let vc : MyUIViewController = storyboard.instantiateViewControllerWithIdentifier("myVCStoryboardID")     as MyUIViewController;
self.presentViewController(vc, animated: true, completion: nil);

这篇关于以编程方式从UIView执行Segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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