IOS Switching the embedded view to the storyboard

I tried to figure this out all day, and I know that it should be done, but new to iOS development using Objective-C, not Appcelerator. I have problems with newbies.

What I'm trying to do is to have a built-in view in one of my other views, but to be able to switch which view is built into programming. I use storyboards. When I tried to use the view container, it displays the attached view, but I cannot attach several views to the container in the storyboard. I compiled the image, but, to my annoyance, I can’t post the image here because I don’t have enough “rep” glasses, so I posted it on my website: http://static.c6gx.com/images/iphone-embedded -views-diagram.jpg

Later, I would like a push segment to be displayed in this container, but first I just want to be able to switch between the built-in representation of 1, 2, 3, etc. Should I use a container view for this or some other type of controller? Then how can I name the transition to change the view?

Thanks for any help you can provide.

+7
source share
3 answers

You can switch view controllers, as you would with a container view controller. I made a project in which I added a container view (actually 2, but we deal only with this here) and added this code to the built-in controller, which you automatically get when dragging and dropping as a container. The controller I'm switching from, NewRightController, is a subclass of UIViewController - in the storyboard, I set the size of the controller to "Freeform" and resized the view to fit the size of the view of the built-in controller. This does not actually affect the size of the view (it still registers as a full screen), it just simplifies the layout of the view.

-(IBAction)switchToNewRight:(id)sender { UIView *rcView = [(ViewController *)self.parentViewController rightView]; // rcView is the right container view in my root view controller that self is embedded in. NewRightController *newRight = [self.storyboard instantiateViewControllerWithIdentifier:@"NewRight"]; newRight.oldRightController = self; // pass self to new controller so I can come back to the same instance newRight.view.frame = rcView.bounds; [self.parentViewController addChildViewController:newRight]; [self moveToNewController:newRight]; } -(void)moveToNewController:(UIViewController *) newController { UIView *rcView = [(ViewController *)self.parentViewController rightView]; [self willMoveToParentViewController:nil]; [self.parentViewController transitionFromViewController:self toViewController:newController duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{} completion:^(BOOL finished) { [self removeFromParentViewController]; [rcView constrainViewEqual:newController.view]; [newController didMoveToParentViewController:self]; }]; } 

After many experiments, I found that the way to correctly resize the views and resize correctly after rotation was to first set the frame of the new view of the controller in the view frame of the container, but then after animating the transition, add restrictions on the new view, which will have size immediately after rotation. Since this code can be used over and over again, I put it in a category in a UIView with one method, constrainViewEqual :. Here is the code for this:

 -(void)constrainViewEqual:(UIView *) view { [view setTranslatesAutoresizingMaskIntoConstraints:NO]; NSLayoutConstraint *con1 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterX relatedBy:0 toItem:view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]; NSLayoutConstraint *con2 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]; NSLayoutConstraint *con3 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:0 toItem:view attribute:NSLayoutAttributeWidth multiplier:1 constant:0]; NSLayoutConstraint *con4 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:0 toItem:view attribute:NSLayoutAttributeHeight multiplier:1 constant:0]; NSArray *constraints = @[con1,con2,con3,con4]; [self addConstraints:constraints]; } 
+6
source

So, after long games, I got him to work, sort of like I imagined. It has no transitions at the moment, but I wanted to publish code to be viewed by others.

In the storyboard, I have a view controller for the entire game, which has a presentation container and a button. The view built into the game view is a page controller that will contain different types of questions. Then the non-attached in the storyboard are different types of question layouts, in my case the Display command and the locationDisplay command.

In QuestionViewController, I added two properties to the .h file:

 @property (nonatomic, strong) UIPageViewController *pageViewController; @property (nonatomic, strong) NSMutableArray *questionTypeViewControllers; 

Method declared:

 -(void)changeView; 

In the .m file, they were synthesized:

 @synthesize questionTypeViewControllers, pageViewController; 

In the viewDidLoad method:

 pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; UIViewController *ldvc = [self.storyboard instantiateViewControllerWithIdentifier:@"locationDisplay"]; UIViewController *tdvc = [self.storyboard instantiateViewControllerWithIdentifier:@"teamDisplay"]; questionTypeViewControllers = [NSMutableArray arrayWithObjects:ldvc, tdvc, nil]; NSArray *initView = [NSArray arrayWithObject:ldvc]; [pageViewController setViewControllers:initView direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; [self addChildViewController:pageViewController]; [self.view addSubview:self.pageViewController.view]; 

And then the changeView method is implemented:

 NSArray *initView = [NSArray arrayWithObject: [questionTypeViewControllers objectAtIndex:1]]; [pageViewController setViewControllers:initView direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; 

In the GameViewController where the button is located, add an action to the button and call the newly created changeView method from QuestionViewController.

 - (IBAction)change:(id)sender { // Need a more reliable way to get the QuestionViewController. [[self.childViewControllers objectAtIndex:0] changeView]; } 

And it's all. No problem?;)

I have no idea if this is suitable for this, but it worked. Any suggestions or improvements are welcome.

+1
source

I wanted to do the same, and this is what I found.

Create custom container view controllers

The bottom line is that you create a navigation controller that you insert into the container view, which allows you to navigate through multiple views as you wish.

Here is a short example / walkthrough:

  • Add a navigation controller to your storyboard.
  • Insert a navigation controller inside the container view.
  • Select the view that will be the root controller of the navigation controller.
  • Create manual segments for each view controller that will be used inside the container view, INCLUDING THE BOX VISION CONTROLLER (I almost made a mistake without adding this). Make sure you name each segue you create.
  • In your code, just call the appropriate manual session when you want to show another view controller.

Hope this helps!

+1
source

All Articles