以编程方式创建segue [英] Creating a segue programmatically

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

问题描述

我有一个共同的 UIViewController ,我所有的 UIViewsControllers 都会扩展以重用一些常见的操作。

I have a common UIViewController that all my UIViewsControllers extend to reuse some common operations.

我想在这个Common UIViewController 上设置一个segue,以便所有其他 UIViewControllers 继承。

I want to set up a segue on this "Common" UIViewController so that all the other UIViewControllers inherit.

我想弄清楚如何以编程方式做到这一点。

I am trying to figure out how do I do that programmatically.

我想这个问题也可以如何为所有 UIViewControllers 设置 segue ,而无需进入故事板并手动完成。

I guess that the question could also be how do I set a segue for all my UIViewControllers without going into the story board and do them by hand.

推荐答案

根据定义,segue不能独立于故事板而存在。它甚至出现在班级名称中: UIStoryboardSegue 。您不能以编程方式创建segues - 它是为您创建它们的storyboard运行时。您通常可以在视图控制器的代码中调用 performSegueWithIdentifier:,但这依赖于已在故事板中设置segue以供参考。

By definition a segue can't really exist independently of a storyboard. It's even there in the name of the class: UIStoryboardSegue. You don't create segues programmatically - it is the storyboard runtime that creates them for you. You can normally call performSegueWithIdentifier: in your view controller's code, but this relies on having a segue already set up in the storyboard to reference.

我认为您要问的是如何在公共视图控制器(基类)中创建一个将转换为新视图控制器的方法,并将由所有派生类继承。您可以通过向基类视图控制器创建类似这样的方法来完成此操作:

What I think you are asking though is how you can create a method in your common view controller (base class) that will transition to a new view controller, and will be inherited by all derived classes. You could do this by creating a method like this one to your base class view controller:

- (IBAction)pushMyNewViewController
{
    MyNewViewController *myNewVC = [[MyNewViewController alloc] init];

    // do any setup you need for myNewVC

    [self presentModalViewController:myNewVC animated:YES];
}

然后在派生类中,单击相应按钮时调用该方法或者选择了表格行等等。

and then in your derived class, call that method when the appropriate button is clicked or table row is selected or whatever.

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

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