从故事板内的 UIViewController 加载 UIView 不起作用 [英] Load an UIView from UIViewController inside storyboard doesn't work
问题描述
我正在尝试从我的 Storyboard
内的 UIViewController
加载一个 UIView
,而没有 segue
.我创建了 UIViewcontroller
,布局了所有东西,将它与特定的类连接起来,并设置了一个 Storyboard ID
.我还将元素连接到我的类的 h.file
,现在我正在寻找一种方法来初始化这个 UIViewController
的 subview
而无需一个 segue
.我一直在搜索并发现了很多以这种方式加载 UIViewController
的帖子:
I am trying to load an UIView
from an UIViewController
inside my Storyboard
without segue
. I created the UIViewcontroller
, layouted everything, connected it with the specific class and set a Storyboard ID
. I also connected the elements to the h.file
of my class and now I am looking for a way to initialize a subview
of this UIViewController
without a segue
. I have been searching and found a lot of posts which loads the UIViewController
this way:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
pointVC = (PointsViewController *)[storyboard instantiateViewControllerWithIdentifier:@"PointsViewController"];
尝试像这样添加视图,视图跳转到下一个UIViewController
:
Try to add the view like this, the view jumps to the next UIViewController
:
pointVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:pointVC animated:YES completion:NULL];
像 addChildViewController
一样添加它也不起作用:
Adding it like addChildViewController
also doesn't work:
[self addChildViewController:pointVC];
初始化后,我尝试将特定 UIViewController
内的 UIViews
之一添加到我的实际视图中,但它不起作用.如果我只使用前两行,则不会发生任何事情,UIViewController
对象内部没有 UIViews
,因为 viewDidLoad
永远不会被调用.有什么想法吗?
after initializing I try to add one of the UIViews
inside the specific UIViewController
to my actual View but it doesn't work. There doesn't happen anything if I just use the first two lines, the UIViewController
object has no UIViews
inside because viewDidLoad
never will be called. Any ideas?
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
pointVC = (PointsViewController *)[storyboard instantiateViewControllerWithIdentifier:@"PointsViewController"];
[self addChildViewController:pointVC];
[self.view addSubview:pointVC.viewPointsDialog];
[pointVC didMoveToParentViewController:self];
推荐答案
应该是
[self.view addSubview:pointVC.view];
代替
[self.view addSubview:pointVC.viewPointsDialog];
所以最终的工作代码应该是这样的:
So final working code should look like this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
pointVC = (PointsViewController *)[storyboard instantiateViewControllerWithIdentifier:@"PointsViewController"];
[self addChildViewController:pointVC];
[self.view addSubview:pointVC.view];
[pointVC didMoveToParentViewController:self];
您可以在 pointVC.view 上使用 setFrame 来正确定位它.
You can use setFrame on pointVC.view to position it properly.
这篇关于从故事板内的 UIViewController 加载 UIView 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!