简体   繁体   English

从故事板内的UIViewController加载UIView不起作用

[英]Load an UIView from UIViewController inside storyboard doesn't work

I am trying to load an UIView from an UIViewController inside my Storyboard without segue . 我试图从我的StoryboardUIViewController加载UIView而不使用segue I created the UIViewcontroller , layouted everything, connected it with the specific class and set a Storyboard ID . 我创建了UIViewcontrollerUIViewcontroller所有内容,将其与特定类连接并设置了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 . 我还将元素连接到我的类的h.file ,现在我正在寻找一种方法来初始化没有segue的这个UIViewControllersubview I have been searching and found a lot of posts which loads the UIViewController this way: 我一直在搜索并发现很多帖子以这种方式加载UIViewController

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    pointVC = (PointsViewController *)[storyboard instantiateViewControllerWithIdentifier:@"PointsViewController"];

Try to add the view like this, the view jumps to the next UIViewController : 尝试添加这样的视图,视图跳转到下一个UIViewController

pointVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:pointVC animated:YES completion:NULL];

Adding it like addChildViewController also doesn't work: addChildViewController一样添加它也不起作用:

[self addChildViewController:pointVC];

after initializing I try to add one of the UIViews inside the specific UIViewController to my actual View but it doesn't work. 在初始化之后,我尝试将特定UIViewController一个UIViews添加到我的实际View中,但它不起作用。 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. 如果我只使用前两行,则不会发生任何事情, UIViewController对象内部没有UIViews ,因为从不会调用viewDidLoad Any ideas? 有任何想法吗?


I also tried 我也试过了

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
pointVC = (PointsViewController *)[storyboard instantiateViewControllerWithIdentifier:@"PointsViewController"];
[self addChildViewController:pointVC];
[self.view addSubview:pointVC.viewPointsDialog];
[pointVC didMoveToParentViewController:self];

It should be 它应该是

[self.view addSubview:pointVC.view];

instead of 代替

[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];

You can use setFrame on pointVC.view to position it properly. 您可以在pointVC.view上使用setFrame来正确定位它。

First of all if you initialise a UIViewController from storyboard using 首先,如果您使用故事板初始化UIViewController

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    pointVC = (PointsViewController *)[storyboard instantiateViewControllerWithIdentifier:@"PointsViewController"];

this will only initialise the view controller, sort of alloc/init method. 这只会初始化视图控制器,排序为alloc/init方法。 So if you try to add a view as a subview of the view controller's view, at this moment, it won't work because the view controller's view is not loaded yet. 因此,如果您尝试将视图添加为视图控制器视图的子视图,此时它将无法工作,因为视图控制器的视图尚未加载。 You have to push/present/addChild the view controller first and after that add the subview. 您必须首先push/present/addChild视图控制器,然后添加子视图。

Second, if you had created segues just use the performSegueWithIdentifier: method to push/present the view controller (there is modal segue), and in your pushed/presented view controller viewWillAppear/viewDidAppear methods you can add the subivew (don't do it in viewDidLoad because the frame of the view might not be properly set yet). 其次,如果你创建了segues,只需使用performSegueWithIdentifier:方法来推送/呈现视图控制器(有模态segue),并在你推/呈现的视图控制器viewWillAppear/viewDidAppear方法中添加subivew(不要这样做)在viewDidLoad因为视图的框架可能尚未正确设置)。

Third, if you are using autolayout, you might have to check the constraints and make sure to disable the conversion from springs to autolayout, because addSubview will enable that conversion. 第三,如果您使用autolayout,则可能必须检查约束并确保禁用从spring转换为autolayout,因为addSubview将启用该转换。

The reason the the elements are all nil is that the elements will be loaded ONLY after the view is to be displayed. 元素全部为零的原因是元素将仅在显示视图后加载。 You can force it to load the elements. 您可以强制它加载元素。 Here is what has worked for me: 这对我有用:

UIViewController *vC = 
 [[UIStoryboard storyboardWithName:@"ViewControllers" 
                            bundle:nil] 
     instantiateViewControllerWithIdentifier:@"MyViewController"];`  
vC.view.frame = self.view.frame;  

You can then display or show the view: 然后,您可以显示或显示视图:

[self presentViewController:vC animated:YES completion:nil];

I had to do this for any view controller that was loaded from storyboard. 我必须为从故事板加载的任何视图控制器执行此操作。 You will not need to do this if the view controller is loaded from nib or xib file. 如果从nib或xib文件加载视图控制器,则无需执行此操作。

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

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