Segment application


 

Segment application

In this application we are going to create Segment Application by using View based application. In this we are using Segmented Controller and will allot different view to different segment.

In this application we are going to create Segment Application by using View based application. In this we are using Segmented Controller and will allot different view to different segment.

iPhone Segment application

 

In this application we are going to create iphone segment application by using View based application. In this we are using Segmented Controller and will allot different view to different segment.

 Final view will look like this:

          

 

Create a new project by select View-Based Application. Open the Interface Builder by double clicking the SegmentViewController.xib file and add a segment from the Tools-> Library.

 in .h file declare a segment and the action

 IBOutlet UISegmentedControl *mySegMent;

IBOutlet UIView *first; 

 IBOutlet UIView *second;

 IBOutlet UIView *third;

is declared as outlet and a variable is created

-(void)segmentAction:(id)sender;

is an action method

.h file will look like this:

#import <UIKit/UIKit.h>

@interface SegmentViewController : UIViewController

{

 IBOutlet UISegmentedControl *myMent;

 IBOutlet UIView *first;  // creating variable for view

 IBOutlet UIView *second; // creating variable for view

 IBOutlet UIView *third; // creating variable for view

}

-(IBAction)segmentAction:(id)sender;

@end

 After declaring connect it in Interface Builder

and now we  are going to write the method for the action in .m file, here we are setting the index value of segment  and in it we are printing the value this is done by using if method.

 - (IBAction) segmentAction:(id)sender

{

0

    UISegmentedControl* segCtl = sender ;

    if( [segCtl selectedSegmentIndex] == 0 )

    {

1

      [ self.view addSubview:first] ;// adding view to segmentindex 0

    }

    if( [segCtl selectedSegmentIndex] == 1 )

2

    {

         [ self.view addSubview:second] ;

    }

3

    if( [segCtl selectedSegmentIndex] == 2 )

    {

      [ self.view addSubview:third] ;

4

}

}

 - (void)viewDidLoad

5

{

   first.frame = CGRectMake(0, 80, 320, 400); // reducing size

   second.frame = CGRectMake(0, 80, 320, 400);

6

   third.frame = CGRectMake(0, 80, 320, 400);

        [super viewDidLoad];

        // the segment control…

7

        [myMent addTarget:self action:@selector(segmentAction:)

        forControlEvents:UIControlEventValueChanged];

    myMent.selectedSegmentIndex = 0 ;

8

 }

  And to load this into the view we are adding

[myMent addTarget:self action:@selector(segmentAction:)

9

  forControlEvents:UIControlEventValueChanged];

  myMent.selectedSegmentIndex = 0 ;

 this into the -(void) viewDidLoad to set the selection of segment to first view or index 0.

0

 .m file will look like this:

 #import "SegmentViewController.h"

 @implementation SegmentViewController

1

 // segment action and reduce size of view

 - (IBAction) segmentAction:(id)sender

{

2

  UISegmentedControl* segCtl = sender ;

  if( [segCtl selectedSegmentIndex] == 0 )

    {

3

[ self.view addSubview:first] ; // adding view to segmentindex 0

    }

   if( [segCtl selectedSegmentIndex] == 1 )

4

    {

         [ self.view addSubview:second] ; // adding view to segmentindex 1

    }

5

    if( [segCtl selectedSegmentIndex] == 2 )

    {

      [ self.view addSubview:third] ; // adding view to segmentindex 2

6

}

}

 - (void)viewDidLoad

7

{

    first.frame = CGRectMake(0, 80, 320, 400); // reduce size of view

    second.frame = CGRectMake(0, 80, 320, 400);// reduce size of view

8

    third.frame = CGRectMake(0, 80, 320, 400);// reduce size of view

        [super viewDidLoad];

        // the segment control…

9

        [myMent addTarget:self action:@selector(segmentAction:)

        forControlEvents:UIControlEventValueChanged];

    myMent.selectedSegmentIndex = 0 ;

0

}

 - (void)didReceiveMemoryWarning

{

1

    // Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];

}

2

 - (void)viewDidUnload {

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

3

}

 - (void)dealloc {

    [super dealloc];

4

}

@end

Make connection in Interface Builder:

5

 

Finally press Build and Go Button

6

Download the Source Code

Ads