How can I create one UIViewController with two views that display one or the other depending on the button clicked

I'm new to iOS development and I'm stuck. I am currently using one tab controller to switch between two view controllers (list and map view). This made it easier to use storyboards to customize the look of the two views.

Now the requirements have changed, and the application should have one view controller with a segmented control that, when clicked, displays either a list or a map view. To do this, I will need to create one view controller that can display the list / map view.

I understand how the segmented part of the controller works, but I just got stuck on how I can have two views with one or the other displayed in the same area. How can I use two views in one view controller (if possible using a storyboard)?

Thanks in advance!

+7
source share
3 answers

You should not have two main views in one view controller, instead you need to create one view controller for each view that you want to show. However, you can have multiple subzones in the same view controller, which may be what works for you.

There are several approaches to solve this problem, the right approach would be to create a UIViewController container and add the 2 hot servers that you want to show as your children, just set the view to the view controller that you want to display, but this, will probably be overly complex as you mention that you are new to iOS development.

So a simple solution (not sure if you can implement this in a storyboard - since I don't like it) would be to have one view controller, with tabs and 2 main view subviews, then you can just switch between views, doing something like this:

[self.view addSubview:view1]; 

// to switch

 [view1 removeFromSuperview]; [self.view addSubView:view2]; 

alternatively, you really do not need to remove it from the supervisor, but just hide it and then use bringSubViewToFront to show the desired view.

If you want to use a different approach, I would recommend that you watch this video on WWDC 2011 video titled “Implementing the UIViewController Containment”. This other question should be useful for: UISegmented control with two views

Hope this helps.

+8
source

Using the api storyboard, you can switch between 2 viewControllers

  - (void)viewDidLoad { [super viewDidLoad]; UIViewController *viewController = [self viewControllerForSegmentIndex:self.typeSegmentedControl.selectedSegmentIndex]; [self addChildViewController:viewController]; viewController.view.frame = self.contentView.bounds; [self.contentView addSubview:viewController.view]; self.currentViewController = viewController; } - (UIViewController *)viewControllerForSegmentIndex:(NSInteger)index { UIViewController *viewController; switch (index) { case 0: viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"]; break; case 1: viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"]; break; } return viewController; } - (IBAction)segmentChanged:(UISegmentedControl *)sender { UIViewController *viewController = [self viewControllerForSegmentIndex:sender.selectedSegmentIndex]; [self addChildViewController:viewController]; [self transitionFromViewController:self.currentViewController toViewController:viewController duration:0.0 options:UIViewAnimationOptionTransitionNone animations:^{ [self.currentViewController.view removeFromSuperview]; viewController.view.frame = self.contentView.bounds; [self.contentView addSubview:viewController.view]; } completion:^(BOOL finished) { [viewController didMoveToParentViewController:self]; [self.currentViewController removeFromParentViewController]; self.currentViewController = viewController ; }]; self.navigationItem.title = viewController.title; } 

This applies to the Raywenderlich iOS tutorial. Hope this helps

+4
source

With a storyboard, this is possible.

  1. Create a UIViewController by adding a UISegmentControl and UITableView + UITableViewCell to it.

storyboard tableview with segment control

  1. Now you also want to add MKMapView, however, if you just try to put the MapView in the ViewController, it will be added as a new TableView cell, which is not what we need.

Storyboard mapview added as tableviewcell

That is why you should not do it like this. Instead, add MapView to the list of storyboards ViewControllers

MapView added to Stroyboard viewcontroller view

  1. Adjust the size and origin of the MapView so that it matches the size of the TableView.

Adjusting size and origin for MapView

  1. Now install Hidden for YES for any TableView MapView, create and synthesize outputs for them. Then, in the Value Changed control segment, we implement the switching:

     - (IBAction)switchView:(id)sender { self.theTableView.hidden = !self.theTableView.hidden; self.theMapView.hidden = !self.theMapView.hidden; if (!self.theTableView.hidden) { [self.theTableView reloadData]; } } 
0
source

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


All Articles