How to load a UIView from a NIB?

泪湿孤枕 提交于 2019-12-03 05:01:35

问题


I have been using UIViewControllers and initWithNibName with much success, basically using them as a convenient way to design the view with Interface Builder. Unfortunately I have built a hierarchy of views before noticing this line in the UIViewController documentation:

Note: You should not use view controllers to manage views that fill only a part of their window

My question is this: Having a very simple NIB that only has a UIView in addition to the default First Responder and Owning Object, what is the simplest way to load the UIView into my code?

I have not been able to get loadNibNamed:owner:options: to work at this point, but suspect the answer will involve it somehow.


回答1:


Yes, just call

[[NSBundle mainBundle] loadNibNamed:@"viewNib" owner:self options:nil];

You normally do this from the view controller you have set as File's Owner in the NIB. That way, you can declare an outlet for the view in the view controller which will automatically get connected when you load the NIB file. You don't even have to work with the return value of the method in this case.



来源:https://stackoverflow.com/questions/2648299/how-to-load-a-uiview-from-a-nib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!