xcode 将 .xib 文件添加到故事板中的 ViewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12572818/
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
Add .xib file to a ViewController in storyboard
提问by Ali
I am somehow new in iPhone development.
我是 iPhone 开发的新手。
I have a storyboard that includes a ViewController. inside the ViewController, I have a UIScrollView.
我有一个包含 ViewController 的故事板。在 ViewController 中,我有一个 UIScrollView。
I want to add two images and other controls to my scrollView. I read this link, and decide to implement the idea that described as the answer, but not the way he did programmatically.
我想向我的 scrollView 添加两个图像和其他控件。我阅读了此链接,并决定实施描述为答案的想法,但不是他以编程方式执行的方式。
I create a new .xib
file and then add a UIView to it and add my UIImages and Labels in it.
我创建一个新.xib
文件,然后向其中添加一个 UIView 并在其中添加我的 UIImages 和 Labels。
Now, I want to add this UIView to my UIScrollView in the storyboard. This is my code:
现在,我想将此 UIView 添加到故事板中的 UIScrollView 中。这是我的代码:
OfferUIView *newPageView = [[OfferUIView alloc] init];
self.newPageView.contentMode = UIViewContentModeScaleAspectFill;
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
[self.pageViews replaceObjectAtIndex:page withObject:newPageView];
It does not work for my UIView which is a .xib file, but simply if I add for instance a UIImageView like:
它不适用于我的 UIView,它是一个 .xib 文件,但如果我添加一个 UIImageView,例如:
UIImageView *newPageView = [[UIImageView alloc] initWithImage:image];
self.newPageView.contentMode = UIViewContentModeScaleAspectFill;
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
[self.pageViews replaceObjectAtIndex:page withObject:newPageView];
Then it perfectly works. So how can I fix my code to add .xib file?
然后它完美地工作。那么如何修复我的代码以添加 .xib 文件?
采纳答案by MikeCocoa
The Answer that Carlos gave probably should work with 1 tweak. It sounds to me like your .xib file might contain just an UIView rather than an UIViewController. If that is the case you would need to do something more like:
卡洛斯给出的答案可能应该与 1 个调整一起工作。在我看来,您的 .xib 文件可能只包含一个 UIView 而不是 UIViewController。如果是这种情况,您需要做更多类似的事情:
NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"NameOfXIB" owner:self options:nil];
UIView *view = [views objectAtIndex:0];
Either that or you would need to change the .xib file to contain an UIViewController, but it doesn't sound like that is quite what you are after.
或者您需要更改 .xib 文件以包含 UIViewController,但这听起来不像您所追求的那样。
回答by CSolanaM
You can try with:
您可以尝试:
UIViewController *temporaryController = [[UIViewController alloc]
initWithNibName:@"NameOfXIB"
bundle:nil];
And then:
进而:
[self.scrollView addSubview:[temporaryController view]];