Storyboards, view controllers, and segmented controls

I have a view controller in a storyboard with segmented control. Segmented control switches the view from three different types of displays. For this, I hide / hide the various elements of the view. This works, but it's hard to edit in the Xcode storyboard editor. I would like to have layers that I can turn on and off depending on the part of the view I'm working on.

Is there any way to do this? Is there a better way?

Update:

I tend to create three views in separate xib files and load them into the view controller view as needed.

+4
source share
1 answer

You can create three different ViewControllers in one storyboard file and give each the storyboard identifier. Do not bind them with segue. Then you drop the container where you want them to appear and delete the automatically created ViewController.

Then the following code works for me:

-(void)loadSubviewAtIndex:(NSUInteger)idx; { [self.subviewController.view removeFromSuperview]; [self.subviewController removeFromParentViewController]; NSString* subviewIdentifier = [self.subviewIdentifiers objectAtIndex:idx]; subviewController = [self.storyboard instantiateViewControllerWithIdentifier:subviewIdentifier]; CGRect frame = self.view.bounds; subviewController.view.frame = frame; [self.view addSubview:self.subviewController.view]; [self addChildViewController:self.subviewController]; } 

Here, I assume that you have a subviewIdentifiers property, which is an NSArray and an implicit mapping of your segmented control index into a storyboard identifier, and an IBOutlet UIViewController* subviewController to which you bind the container view. Just call this method from a segmented control action.

+2
source

Source: https://habr.com/ru/post/1438181/


All Articles