简体   繁体   English

从XIB内的XIB文件中为ViewController视图加载自定义UIView

[英]Loading a custom UIView from a XIB file within a XIB for a ViewController view

I have an WizardSequenceViewController with an IBOutlet WizardView *_wizardView . 我有一个WizardSequenceViewControllerIBOutlet WizardView *_wizardView In many WizardSequenceViewController.xib file I have the view outlet connected to the File's Owner - WizardSequenceViewController . 在许多WizardSequenceViewController.xib文件中,我将view出口连接到文件的Owner- WizardSequenceViewController I have a subview of that view defined with the class attribute set to WizardView . 我有一个该视图的子视图,该视图的class属性设置为WizardView I have connected that WizardView to the outlet in the File's Owner. 我已经将该WizardView连接到文件所有者的插座。 Finally, in my WizardView.xib I have a UILabel that I have placed in the file to test if the view is being rendered. 最后,在WizardView.xib我有一个UILabel放置在文件中以测试是否正在渲染视图。 When I select the WizardSequenceViewController from my tab bar, I see the superview view but not the subview _wizardView . 当我从选项卡栏中选择WizardSequenceViewController ,我看到的是_wizardView视图view而不是子视图_wizardView When I set a breakpoint in my -(id)initWithCoder method in my WizardView.m file I see it stop there, so I know that it is calling that initializer (and thus it should be using the xib to load that file). 当我在WizardView.m文件的-(id)initWithCoder方法中设置断点时,我看到该断点在那里停止,因此我知道它正在调用该初始化程序(因此它应该使用xib加载该文件)。 I have tried many iterations and variations to get this thing to work but I can't and I am going crazy. 我已经尝试了许多迭代和变体来使此功能正常工作,但是我做不到,而且我会发疯。 Does anybody have any ideas? 有人有什么想法吗?

From Apple doc "View Controller Basics, About Custom View Controllers": 从Apple文档“ View Controller基础知识,关于自定义View Controller”:

The one-to-one correspondence between a view controller and the views in its view hierarchy is the key design consideration. 视图控制器与其视图层次结构中的视图之间的一一对应关系是关键的设计考虑因素。 You should not use multiple custom view controllers to manage different portions of the same view hierarchy. 您不应使用多个自定义视图控制器来管理同一视图层次结构的不同部分。 Similarly, you should not use a single custom view controller object to manage multiple screens worth of content. 同样,您不应使用单个自定义视图控制器对象来管理多个屏幕上的内容。

Note: If you want to divide a view hierarchy into multiple subareas and manage each one separately, use generic controller objects (custom objects descending from NSObject) instead of view controller objects to manage each subarea. 注意:如果要将视图层次结构划分为多个子区域并分别管理每个子区域,请使用通用控制器对象(从NSObject降级的自定义对象)而不是视图控制器对象来管理每个子区域。 Then use a single view controller object to manage the generic controller objects. 然后,使用单个视图控制器对象来管理通用控制器对象。

Maybe you can't do a view-and-subview outlet setup in a view controller. 也许您无法在视图控制器中进行视图和子视图插座设置。 And I'm not sure assigning the subview outlet to a separate NSObject subclass would work either, because how would you present it? 而且我不确定将子视图出口分配给单独的NSObject子类也可以,因为您将如何呈现它? Could you write your subview programmatically, using initWithFrame and addSubview, instead of making it an outlet? 您可以使用initWithFrame和addSubview以编程方式编写子视图,而不是使其成为插座吗? Or, if you really want to set it up graphically, could you assign it to a separate view controller as owner? 或者,如果您真的想以图形方式进行设置,是否可以将其作为所有者分配给单独的视图控制器? Then the top view controller will call presentModal on the sub view controller. 然后,顶视图控制器将在子视图控制器上调用presentModal Or, if all you need is a UILabel as a subview, just add the label to the main view? 或者,如果您只需要将UILabel作为子视图,则只需将标签添加到主视图即可?

Even I faced a similar issue. 甚至我也面临类似的问题。 But got it resolved by following steps given in the following link. 但是通过以下链接中给出的步骤解决了该问题。 Hope it helps. 希望能帮助到你。 http://blog.yangmeyer.de/blog/2012/07/09/an-update-on-nested-nib-loading http://blog.yangmeyer.de/blog/2012/07/09/an-update-on-nested-nib-loading

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

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