0

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

  • Can you tell what have you achieved so far? – Vivek Molkar May 25 '15 at 10:15
  • i have tried using four different views separately very bvasically but it looks bad in view that switches position differs in each view –  May 25 '15 at 11:30
  • Have you looked at TabBarController? or do you want to achieve it using segment control? – Vivek Molkar May 25 '15 at 11:34
  • i want to achieve it using segment control –  May 25 '15 at 11:35
  • also i have tried displaying different colours in single view using segment control but i couldnt display other view using this!!! also i am new in this topic and xcode –  May 25 '15 at 11:38
  • See this tutorial might help [link](http://www.edumobile.org/ios/multiple-views-from-a-segmented-control-for-iphone/) – Vivek Molkar May 25 '15 at 11:41
  • yeah thanks it helped me!!!!! :) but how can i change the size of letters to name in segments it clashes or some letters cant be shown due to long name in title!! i searched there is no font size available in segmented control. –  May 25 '15 at 12:46
  • hi i wanna change the background color of button . Ex: when i select the color changes and higlighted and when i click other button previous button should return to normal state and clicked button color should change and highlighted ..Any possibilties pls –  May 26 '15 at 08:29

2 Answers2

0

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.

Simon Degn
  • 901
  • 1
  • 12
  • 40
  • i have tried using four differebnt view controllers by designing buttons in all view controllers when selected it should look alike selected . but that seems to be wrong.. since while i run the app view for each button position differs.. Could you please post a sample code and i am using xcode 4.2 –  May 25 '15 at 11:34
  • There's an example for you in the post above now :) – Simon Degn May 25 '15 at 11:55
  • yup i worked it out it helps ..! but how can i change the size of letters to name in segments it clashes or some letters cant be shown due to long name in title!! i searched there is no font size available in segmented control –  May 25 '15 at 12:48
  • 1
    Cool. Please upvote my answer then :-) - See how you change font size in segmentedControl here: http://stackoverflow.com/questions/2280391/change-font-size-of-uisegmentedcontrol – Simon Degn May 25 '15 at 12:49
0

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];
  • could you please send a sample code. i have tried using four different views codings separately in each view controller to display views –  May 25 '15 at 11:32
  • You don't want to create different view controllers for each screen. Use single view controller and add all views on it. – Afsal Meerankutty May 25 '15 at 11:37
  • sample code for adding multiple views to a viewcontroller view added in answer. Mark it as answer if helps – Afsal Meerankutty May 25 '15 at 11:44