1

Sorry for novice question if it is.

I have my controller/view with UISegmentedControl. I need to switch between controllers/views to let the views take the rest of the screen. The idea is pretty similar to UITabBarController but switching controls are not tab buttons but segment controls. How can i achieve it according to iOS design and practice?

4ntoine
  • 19,816
  • 21
  • 96
  • 220

3 Answers3

0

You can take base view on that add a UISegmentedControl and handle the click event in BASE view controller. On segment control click event just add and remove the respective view controller on the base view.

Saurav Nagpal
  • 1,247
  • 1
  • 10
  • 27
  • can you provide an example? i need to have separate controller/view for each "section" – 4ntoine Jun 26 '14 at 06:40
  • Please go through the following link it describe the same http://stackoverflow.com/questions/9110567/switching-viewcontrollers-with-uisegmentedcontrol-in-ios5 – Saurav Nagpal Jun 26 '14 at 06:54
0

Create a container viewController and include tis logics:

@property (nonatomic, strong) UISegmentedControl *segmentedControl;
@property (nonatomic, strong) NSMutableArray *viewControllers;
@property (nonatomic, weak) UIView *containerView;
@property (nonatomic) NSInteger currentSelectedIndex;



_segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
[_segmentedControl addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventValueChanged];


- (void)changeViewController:(UISegmentedControl *)segmentedControl
{
    UIViewController *oldViewController = self.viewControllers[self.currentSelectedIndex];
    UIViewController *newViewController = self.viewControllers[segmentedControl.selectedSegmentIndex];

    [self willTransitionToViewController:newViewController];
    [self transitionFromViewController:oldViewController
                      toViewController:newViewController
                              duration:0
                               options:UIViewAnimationOptionTransitionNone
                            animations:nil
                            completion:^(BOOL finished) {
                                if (finished)
                                    [self didTransitionToViewController:newViewController];
                            }];
}


- (void)showFirstViewController
{
    UIViewController *firstViewController = [self.viewControllers firstObject];

    // set required the frame
    firstViewController.view.frame = self.containerView.bounds;
    firstViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

    [self.containerView addSubview:firstViewController.view];

    [self willTransitionToViewController:firstViewController];
    [self didTransitionToViewController:firstViewController];
}



- (void)willTransitionToViewController:(UIViewController *)viewController
{
    if (self.currentSelectedIndex != UISegmentedControlNoSegment)
    {

        UIViewController *oldViewController = self.viewControllers[self.currentSelectedIndex];
        [oldViewController willMoveToParentViewController:nil];
    }

    viewController.containerView.frame = self.ContainerView.bounds;
    viewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
}

- (void)didTransitionToViewController:(UIViewController *)viewController
{
    [viewController didMoveToParentViewController:self];

    self.segmentedControl.selectedSegmentIndex = [self.viewControllers indexOfObject:viewController];
    self.currentSelectedIndex = [self.viewControllers indexOfObject:viewController];

}
Yatheesha
  • 10,412
  • 5
  • 42
  • 45
  • will this set new selected controller as current and show its view to the whole screen ? – 4ntoine Jun 26 '14 at 06:49
  • Yes . you need to add segment control to view and also add one more container view below the segment control and add viewControllers view to container view. – Yatheesha Jun 26 '14 at 06:53
  • no, i need to make the view take only part of the screen below segmented control and not just set new controller/view. – 4ntoine Jun 26 '14 at 06:57
  • Read last Comment , i explained how to make make the view take only part of the screen below segmented control.I edited Code also. – Yatheesha Jun 26 '14 at 07:05
  • what is `UIView *containerView;` and how is it initialized? – 4ntoine Jun 26 '14 at 07:30
  • _containerView = [[UIView alloc] initWithFrame:frameBelowSegmentControl]; and [self.view addSubView:_containerView]; – Yatheesha Jun 26 '14 at 07:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56320/discussion-between-yatheesha-and-4ntoine). – Yatheesha Jun 26 '14 at 07:34
0

What you can do, Have one Main ViewController with segment, SayMainViewC and take two different View Controllers say ViewAVC and ViewBVCinside this MainViewC

Add All your subviews that you want to show in ViewA and ViewB.

Add these View Controllers to MainViewC with setting a proper frame for each ViewController.

Now while you add the segmentControl You can add a target function in the following way

itemArray = [NSMutableArray arrayWithObjects: @"Zero", @"One", nil];
seg = [[UISegmentedControl alloc] initWithItems:itemArray];
[seg setFrame:segRect];
seg.segmentedControlStyle = UISegmentedControlStyleBar;
seg.momentary = NO;
[seg addTarget:self action:@selector(someAction:) forControlEvents: UIControlEventValueChanged];
[mainView addSubview:seg];

In this function someAction: check what is the currently selected value of the segment and Hide one view and unhide other.

This way you will have seperate View Controller, but when you wish to interact between these View Controllers and MainViewC you will require to add a few delegates

Ganesh Somani
  • 2,280
  • 2
  • 28
  • 37