ios 如何创建在 XIB 中定义的视图控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18523645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:54:32  来源:igfitidea点击:

How can I create view controller that is defined in XIB?

iosuiviewcontrollerinterface-builderios7

提问by expert

Sorry for perhaps noob question, guys. I'm new to iOS development.

对不起,也许是菜鸟问题,伙计们。我是 iOS 开发的新手。

How can I create view controller that is defined in XIB ?

如何创建在 XIB 中定义的视图控制器?

When you create new class inherited from UIViewControllerin Xcode via "New File" and mark with XIBcheckbox it creates xib file that doesn't have viewControllerelement. But rather it has placeholderwhich points to UIViewControllerwhich is defined in the code.

当您UIViewController通过“新建文件”创建从Xcode继承的新类并标记with XIB复选框时,它会创建没有viewController元素的xib 文件。而是它具有placeholderwhich 指向UIViewControllerwhich在代码中定义。

But I see that you can create controller itself in Interface Builder and specify customClass for it.

但是我看到您可以在 Interface Builder 中创建控制器本身并为其指定 customClass 。

Unfortunately it doesn't work for me. I get loaded the "EmbeddedViewController" nib but the view outlet was not seterror in run-time. But I believed viewproperty must be set by resource loader automatically since everything is defined via XIB. It works that way for TableView and its cells for example.

不幸的是它对我不起作用。我loaded the "EmbeddedViewController" nib but the view outlet was not set在运行时出错。但我相信view属性必须由资源加载器自动设置,因为一切都是通过 XIB 定义的。例如,它适用于 TableView 及其单元格。

Could you please provide example how to do it ?

你能举例说明怎么做吗?

I created sample project: https://github.com/cppexpert/SampleWithNib

我创建了示例项目:https: //github.com/cppexpert/SampleWithNib

The controller in question is EmbeddedViewController

有问题的控制器是EmbeddedViewController

采纳答案by expert

Turned out these controls exist in IB for Storyboard projects.

原来这些控件存在于 IB 中用于 Storyboard 项目。

回答by LE SANG

Create one Xib per UIViewController

每个 UIViewController 创建一个 Xib

Each Xib have File's Owner object for you to set UIViewController class there. Click File's Owner and choose EmbeddedViewController on it's class

每个 Xib 都有 File's Owner 对象供您在那里设置 UIViewController 类。单击 File's Owner 并在其类上选择 EmbeddedViewController

Then drag main UIView not UIViewController class to there, then hook up this view with file's owner as view. UIViewController just use to drag to StoryBoard base project.

然后将主 UIView 而不是 UIViewController 类拖到那里,然后将此视图与文件所有者作为视图连接起来。UIViewController 只是用来拖拽到 StoryBoard 基础项目。

https://github.com/lequysang/gitfiles02/blob/master/SampleWithNib-master.zip

https://github.com/lequysang/gitfiles02/blob/master/SampleWithNib-master.zip

回答by cahn

When you create a view controller with xib via "new file", Xcode generates an UIViewand connects it with viewoutlet automatically. Seems like you changed something after Xcode generated the xib file. In that case you need to connect a view to the viewoutlet manually.

当您通过“新文件”创建带有 xib 的视图控制器时,Xcode 会生成一个UIViewview自动将其与插座连接。似乎您在 Xcode 生成 xib 文件后更改了某些内容。在这种情况下,您需要view手动将视图连接到插座。

回答by kumar123

If you create a new view in xib.set the file owner to your custom class. Or if you simply want to create a view. Then

如果您在 xib.set 中创建一个新视图,请将文件所有者设置为您的自定义类。或者,如果您只是想创建一个视图。然后

NSArray* test1 = [[NSBundle mainBundle] loadNibNamed:@"View" owner:self options:nil];
self.myViewFromNib = [test1 objectAtIndex:0];

Where myViewFromNib is your view object and "View" is your nib name.

其中 myViewFromNib 是您的视图对象,“View”是您的笔尖名称。