Share on Google+Share on Google+

Segment application

Advertisement
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 )

    {

        NSLog(@"2");

    }

    if( [segCtl selectedSegmentIndex] == 2 )

    {

        NSLog(@"31");

    }

}

And to load this into the view we are adding

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

        forControlEvents:UIControlEventValueChanged];

    myMent.selectedSegmentIndex = 0 ;

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

.m file will look like this:

#import "SegmentViewController.h"

#import "abc.h"

@implementation SegmentViewController

// segment action

- (void) segmentAction:(id)sender

{

    UISegmentedControl* segCtl = sender;

    if( [segCtl selectedSegmentIndex] == 0 )

    {

        NSLog(@"1");

    }

    if( [segCtl selectedSegmentIndex] == 1 )

    {

        NSLog(@"2");

    }

    if( [segCtl selectedSegmentIndex] == 2 )

    {

        NSLog(@"31");

    }

}

- (void)viewDidLoad

{

    [super viewDidLoad];

    // the segment control…

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

        forControlEvents:UIControlEventValueChanged];

    myMent.selectedSegmentIndex = 0 ;  

}

- (void)didReceiveMemoryWarning

{

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

    [super didReceiveMemoryWarning];  

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

}

- (void)viewDidUnload

{

    // Release any retained subviews of the main view.

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

}

- (void)dealloc

{

    [super dealloc];

}

@end

Download the Source Code

Advertisements

Advertisement

Posted on: August 30, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.