Home Tutorial Iphone Examples iPhone Segmented Controller Through Coding

 
 

Share on Google+Share on Google+
iPhone Segmented Controller Through Coding
Posted on: October 9, 2009 at 12:00 AM
Advertisement
In this Tutorial will learn how to create Segmented Controller through coding and also how to set on to the view with different color and different size, this application is View Based Application .

iPhone Segmented Controller Through Coding

In this Tutorial will learn how to create Segmented Controller through coding and also how to set on to the view with different color and different size, this application is View Based Application .

Final Image will look like this:

My project name VariousSegmentedController, is based on View Based application. Till now you have seen the Segment Controller which we are using in our application is using Interface Builder but here am not using the Interface Builder, we are going to create the Segmented controller and will set the frame size, length, margin and many more. First will define some constant values which we are going to use in our application, value can be changed also the name.
After defining the constant values, will create the Segmented Controller and also will write the text on the segment which we want to display on to the Segmented Controller segments, after that will set the margin for the Segmented Controller, size, height and frame. Then will also set the selected Segment Index so that one of the segment of Segmented Controller is selected when you run the application or the project similarly will do for the others segmented Controller, will add one thing to that is, color to the Segmented Controller. After all this will add creat method to the viewDidLoad method and will write the action method for the segment of Segmented Controller so that when the segment is pressed the application can run and can so something action for each of the segment in Segmented Controller. Since we are not using it from Interface Builder therefore there is no need to connect it with the variable.

Add this to ViewController.m File:

#define mSegmentedControlHeight 30.0

#define mLabelHeight                25.0

#define mLeftMargin                       20.0 

#define mRightMargin                20.0

#define mTweenMargin                20.0

- (void)creat

{

 NSArray *text = [NSArray arrayWithObjects: @"Segment", @"Controlar", @"Coding", nil];

 CGFloat yPlacement = (mTweenMargin * 3.0) + mSegmentedControlHeight;

 CGRect frame=CGRectMake(mLeftMargin, yPlacement, self.view.bounds.size.width-(mRightMargin * 3.0),mLabelHeight);

 UISegmentedControl *control= [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects: nil]];

control = [[UISegmentedControl alloc] initWithItems:text];

    control.frame = frame;

    control.selectedSegmentIndex = 0;

    [self.view addSubview:control];

    [control release];

 yPlacement += (mTweenMargin * 3.0) + mSegmentedControlHeight;

    control = [[UISegmentedControl alloc] initWithItems:text];

    frame = CGRectMake( mLeftMargin,yPlacement,self.view.bounds.size.width-(mRightMargin * 2.0),mSegmentedControlHeight);

    control.frame = frame;

    control.segmentedControlStyle = UISegmentedControlStyleBar;  

    control.tintColor = [UIColor colorWithRed:0.80 green:0.171 blue:0.5 alpha:1.0];

    control.selectedSegmentIndex = 1;

    [self.view addSubview:control];

    [control release];

}

- (void)viewDidLoad

{  

    [super viewDidLoad];

    [self creat];

}

- (void)segmentAction:(id)sender

{}

Finally press Build And Go Button

Download Code

Advertisement

Related Tags for iPhone Segmented Controller Through Coding:


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: October 9, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com