Home Tutorial Iphone Examples Segment application

 
 

Share on Google+Share on Google+
Segment application
Posted on: September 3, 2009 at 12:00 AM
Advertisement
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

{

    UISegmentedControl* segCtl = sender ;

    if( [segCtl selectedSegmentIndex] == 0 )

    {

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

    }

    if( [segCtl selectedSegmentIndex] == 1 )

    {

         [ self.view addSubview:second] ;

    }

    if( [segCtl selectedSegmentIndex] == 2 )

    {

      [ self.view addSubview:third] ;

}

}

 - (void)viewDidLoad

{

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

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

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

        [super viewDidLoad];

        // the segment control…

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

        forControlEvents:UIControlEventValueChanged];

    myMent.selectedSegmentIndex = 0 ;

 }

  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"

 @implementation SegmentViewController

 // segment action and reduce size of view

 - (IBAction) segmentAction:(id)sender

{

  UISegmentedControl* segCtl = sender ;

  if( [segCtl selectedSegmentIndex] == 0 )

    {

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

    }

   if( [segCtl selectedSegmentIndex] == 1 )

    {

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

    }

    if( [segCtl selectedSegmentIndex] == 2 )

    {

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

}

}

 - (void)viewDidLoad

{

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

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

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

        [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];

}

 - (void)viewDidUnload {

    // Release any retained subviews of the main view.

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

}

 - (void)dealloc {

    [super dealloc];

}

@end

Make connection in Interface Builder:

 

Finally press Build and Go Button

Download the Source Code

Advertisement

Related Tags for Segment application :


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.

Posted on: September 3, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com