简体   繁体   中英

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 . 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"];

Try to add the view like this, the view jumps to the next UIViewController :

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

Adding it like addChildViewController also doesn't work:

[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. 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?


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.

First of all if you initialise a UIViewController from storyboard using

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. 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.

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).

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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