Segment application


 

Segment application

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.

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.

Segment application

In this application we are going to create it by using View based application.

 

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;

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;

}

-(void)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.

- (void) segmentAction:(id)sender

{

    UISegmentedControl* segCtl = sender;

    if( [segCtl selectedSegmentIndex] == 0 )

    {

        NSLog(@"1");

    }

    if( [segCtl selectedSegmentIndex] == 1 )

    {

0

        NSLog(@"2");

    }

    if( [segCtl selectedSegmentIndex] == 2 )

1

    {

        NSLog(@"31");

    }

2

}

And to load this into the view we are adding

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

3

        forControlEvents:UIControlEventValueChanged];

    myMent.selectedSegmentIndex = 0 ;

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

4

.m file will look like this:

#import "SegmentViewController.h"

#import "abc.h"

5

@implementation SegmentViewController

// segment action

- (void) segmentAction:(id)sender

6

{

    UISegmentedControl* segCtl = sender;

    if( [segCtl selectedSegmentIndex] == 0 )

7

    {

        NSLog(@"1");

    }

8

    if( [segCtl selectedSegmentIndex] == 1 )

    {

        NSLog(@"2");

9

    }

    if( [segCtl selectedSegmentIndex] == 2 )

    {

0

        NSLog(@"31");

    }

}

1

- (void)viewDidLoad

{

    [super viewDidLoad];

2

    // the segment control…

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

        forControlEvents:UIControlEventValueChanged];

3

    myMent.selectedSegmentIndex = 0 ;  

}

- (void)didReceiveMemoryWarning

4

{

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

    [super didReceiveMemoryWarning];  

5

    // Release any cached data, images, etc that aren't in use.

}

- (void)viewDidUnload

6

{

    // Release any retained subviews of the main view.

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

7

}

- (void)dealloc

{

8

    [super dealloc];

}

@end

9

Download the Source Code

Ads