switching views between views in ios using segmented control

2k views Asked by At

friends,

I need to switch between four to five views off different headers

There are four views Example Settings Connections Open trades close trades

These are the headers I want to navigate between four pages where i click

For example I want to switch to settings view when i click on it similarly all other views but those buttons must be in all views

but i need these buttons in only one view. While I select it should switch to other views

2

There are 2 answers

4
Simon Degn On BEST ANSWER

Depending on how demanding the content four views is, I would suggest to make one main view for the segmented control and set up four container views in the main view. The three of them should be hidden and then you can toggle between the four views (show/hide).

This is only a good solution if the views' codes are very "soft" or it'll be very slow to run 4-5 views at the same time. If it's four hardcore views I would prefer to use the standard navigation tab bar control instead..

//////// EXAMPLE ////////

The setup will be with one UIViewController for the background. On this view we will place one UISegmentedControl + four container views. The four container views should be placed on the top of each other. The three of the container views are hidden so you only see one.

BackgroundViewController.h:

#import <UIKit/UIKit.h>

@interface BackgroundViewController : UIViewController {
    IBOutlet UISegmentedControl *segmentedControl;
    UIView actualView;
}

@property (nonatomic, weak) IBOutlet UIView *containerOne;
@property (nonatomic, weak) IBOutlet UIView *containerTwo;
@property (nonatomic, weak) IBOutlet UIView *containerThree;
@property (nonatomic, weak) IBOutlet UIView *containerFour;

@end

Here's an example of the IBAction for the segmented control.

- (void) viewDidLoad {
    actualView = self.containerOne;
    UIView *fromView = nil;
    UIView *toView = nil;

    self.containerOne.hidden = NO;
    self.containerTwo.hidden = YES;
    self.containerThree.hidden = YES;
    self.containerFour.hidden = YES;
}

- (IBAction)segmentSwitchClick {
    NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;

    UIView *fromView = actualView;
    UIView *toView = nil;

    switch (selectedSegment) {
        case 0: {
            toView = [self containerOne];
            break;
        } 
        case 1: {
            toView = [self containerTwo];
            break;
        }  
        case 2: {
            toView = [self containerThree];
            break;
        }  
        case 3: {
            toView = [self containerFour];
            break;
        }  
        default:
            break;
        }
    }
    [UIView transitionFromView:fromView toView:toView duration:1.9 options:UIViewAnimationOptionShowHideTransitionViews |  UIViewAnimationOptionCurveLinear
                completion:^(BOOL finished) {
                    if (finished) {
                        actualView = toView;
                    }
                }];

}

PS I haven't tried it, but it should work.

3
Afsal Meerankutty On

Add Segmented Control in main content view. Then add other views as subviews under the segmented control. (Set the frame of subviews in order to make that views do not overlap the segmented control) Then set IBOutlet for each subview. In action method for segmented control show and hide subviews based on segmented control selected index. When you need to show a view, hide other sub views.

This is a simple straight forward solution

Below is sample code for adding 3 views on super view of viewcontroller (not tested)

CGRect frame = CGRectMake(x, y, width, height); // Replacing with your dimensions
UIView *view1 = [[UIView alloc] initWithFrame:frame];
UIView *view2 = [[UIView alloc] initWithFrame:frame];
UIView *view3 = [[UIView alloc] initWithFrame:frame];

Then, you want to actually add it to the superview (assuming the view is self.view)

[self.view addSubview:view1];
[self.view addSubview:view2];
[self.view addSubview:view3];