简体   繁体   English

具有Storyboard的UIViewController子类的子类无法正常工作

[英]Subclass from the subclass of UIViewController with Storyboard doesn't work as except

I have two view controllers, one subclass UIViewController 我有两个视图控制器,一个子类UIViewController

class MGStatusViewController: UIViewController

with storyboard identifier " MGStatusViewController ".(With some UIButton s) 带有故事板标识符“ MGStatusViewController ”。(带有某些UIButton

And the other subclass **MGStatusViewController** 还有另一个子类**MGStatusViewController**

class MGStatusDetailViewController: MGStatusViewController

with storyboard identifier MGStatusDetailViewController 使用情节MGStatusDetailViewController标识符MGStatusDetailViewController

When i use it like this: 当我这样使用它时:

self.storyboard?.instantiateViewControllerWithIdentifier("MGStatusDetailViewController") as? MGStatusDetailViewController

There's no controllers(the UIButton s) from MGStatusViewController's storyboad file. MGStatusViewController的storyboad文件中没有控制器( UIButton )。

How should i achieve this? 我应该如何实现呢? Thanks! 谢谢!

When you inherit your custom view controller - in this example MGStatusViewController you inherit code behind and logic. 当您继承自定义视图控制器时-在此示例MGStatusViewController您将继承代码和逻辑。 You can connect your outlets for MGStatusDetailViewController with MGStatusViewController . 您可以将MGStatusDetailViewController的插座与MGStatusViewController And that is it. 就是这样。 You can't inherit UIView controls from the storyboard and expect them to show. 您不能从情节提要中继承UIView控件,而不能期望它们显示出来。

What you can do is to create MGStatusDetailViewController take the view from it and add it to the MGStatusViewController . 您可以做的是创建MGStatusDetailViewController获取视图并将其添加到MGStatusViewController Or, you can create embed control and embed MGStatusDetailViewController in MGStatusViewController . 或者,您可以创建嵌入控件并将MGStatusDetailViewController嵌入MGStatusViewController This way you will have all the controls from storyboard and will present your MGStatusDetailViewController 这样,您将拥有情节MGStatusDetailViewController所有控件,并将展示您的MGStatusDetailViewController

In short, inheritance does not take Views from storyboard, only code from that view controller. 简而言之,继承不会从情节提要中获取视图,而仅从该视图控制器获取代码。

Furthermore, when you inherit from view controllers, you use them as an abstract. 此外,当您从视图控制器继承时,可以将它们用作抽象。 You create some logic for that will be used on others view controllers, but you don't create actual UI. 您为此创建了一些逻辑,这些逻辑将在其他视图控制器上使用,但是您没有创建实际的UI。

Why you use "MGStatusDetailViewController" story board identifier if you have set identifier as "MGStatusViewController" 如果将标识符设置为“ MGStatusViewController”,为什么要使用“ MGStatusDetailViewController”故事板标识符

Storybord ID and class are diferent thing. Storybord ID和类是不同的东西。

You may try with this - 您可以尝试一下-

self.storyboard?.instantiateViewControllerWithIdentifier("MGStatusViewController") as? MGStatusDetailViewController 

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

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