iosxcode4.2uisegmentedcontrolsegmentedcontrol

switching views between views in ios using segmented control


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


Solution

  • 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.