简体   繁体   English

从Storyboard用Xib调用View Controller?

[英]Call view controller with xib from storyboard?

For example, I want to create a button which will always push a simple view controller of the same class. 例如,我要创建一个按钮,该按钮将始终按相同类的简单视图控制器。

Of course I can place it in the same storyboard file with other view controllers. 当然,我可以将其与其他视图控制器放在同一故事板文件中。 But it could be called from everywhere. 但是它可以从任何地方调用。 So it means too many extra links in this storyboard. 因此,这意味着此情节提要中有太多额外的链接。

Contrawise I didn't try to create another storyboard, not xib. 相反,我没有尝试创建另一个情节提要,而不是xib。 But the view controller I need is so simple that the creating of a new storyboard for it looks like too extra. 但是我需要的视图控制器是如此简单,以至于为其创建一个新的情节提要似乎太多余了。

You can invoke a view controller using an identifier instead of a unique identifier. 您可以使用标识符而不是唯一标识符来调用视图控制器。 Take a look a the method instantiateViewControllerWithIdentifier: 看看方法InstantiateViewControllerWithIdentifier:

That enables you to create view controllers that "float" in your storyboard, without having segue links cluttering up the storyboard. 这样一来,您就可以创建“浮动”在情节提要中的视图控制器,而不会导致segue链接混乱在情节提要中。

Once you've instantiated the view controller, you can then present it modally with presentViewController:animated:completion, push it onto a navigation controller, or whatever. 实例化视图控制器后,可以使用presentViewController:animated:completion模态呈现它,将其推送到导航控制器上,或其他方式。

You can even instantiate a view controller with instantiateViewControllerWithIdentifier, then create your own segue object using initWithIdentifier:source:destination, and invoke that segue if you want to. 您甚至可以使用InstantiateViewControllerWithIdentifier实例化视图控制器,然后使用initWithIdentifier:source:destination创建自己的segue对象,并根据需要调用该segue。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM