Translate

Monday, April 2, 2012

Programmatically Segmented Control in iPhone

In this application we will see how to implement Segmented Control programmatically in iPhone. So let see how it will
worked.
Step 1: Open the Xcode, Create a new project using View Base application. Give the application
“ProgrammaticallySegmentedControl”.
Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the
directory structure to check out the content of the directory.
Step 3: Expand classes and notice Interface Builder created the FirstViewController and SecondViewController class
for you. Expand Resources and notice the template generated a separate nib,
ProgrammaticallySegmentedControlViewController.xib for the ProgrammaticallySegmentedControl application.
Step 4: Open the ProgrammaticallySegmentedControlViewController.m file and make the following changes:

#import "programmaticallySegmentedControlViewController.h"
#define HeightSegmControl 40.0
#define TextHeight 30.0
#define LeftMargin 30.0
#define RightMargin 30.0
#define TweenMargin 30.0
@implementation programmaticallySegmentedControlViewController
- (void)SegmentControl
{
NSArray *text = [NSArray arrayWithObjects: @" Segmented", @"Control", @"iPhone", nil];
CGFloat yPlacement = (TweenMargin * 3.0) + HeightSegmControl;
CGRect frame=CGRectMake(LeftMargin, yPlacement, self.view.bounds.size.width-(RightMargin *
3.0),TextHeight);
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];
// label
//yPlacement += (mTweenMargin * 3.0) + mSegmentedControlHeight;
control = [[UISegmentedControl alloc] initWithItems:text];
frame = CGRectMake(LeftMargin,yPlacement,self.view.bounds.size.width-(RightMargin *
2.0),HeightSegmControl);
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)dealloc
{
[super dealloc];
}
- (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.
}
#pragma mark – View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
[self SegmentControl];
}
- (void)segmentAction:(id)sender
{}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
 
 
Step 5: Now Compile and run the application on the Simulator.




You can Download SourceCode from here
 
 

No comments:

Post a Comment