Friday, November 23, 2012

SegmentControl Application in iPhone

In this application we will see how to implement Segmented Controller in the application. So let let see how it will worked. My last post you can find out from here (Please attached the link of the previous post)

Step 1: Open the Xcode, Create a new project using View Base application. Give the application “SegmentControl”.

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: We need to add two resources in the Resource folder.

Step 4: Open the SegmentControlViewController.h file and make the following changes.

#import
@interface SegmentControlViewController : UIViewController {
IBOutlet UISegmentedControl *segment;
IBOutlet UIView *firstView;
IBOutlet UIView *secondView;
}
- (IBAction )segmentControlAction : ( id )sender;
@end

Step 5: Double click the SegmentControlViewController.xib file and open it to the Interface Builder. First drag the Segmented Control from the library and place it to the view window. Select the Segmented control and bring up Connection Inspector and connect Touch Up Inside to the File Owner icon and select segmentControlAction: , connect File’s Owner icon to the Segmented Control and select segment. Now drag the View two times from the library and place it to the Interface window. Drag the imageView from the library and place it to the View window and select the view and bring up Attribute Inspector , select the “bg.png” image. Do it once more time and select the “manu-screen.png”. Connect File’s owner icon to the View and select firstView. Select File’s Owner icon to the view and select secondView. Now save the .xib file, close it and go back to the Xcode.

Step 6: In the SegmentControlViewController.m file make the following changes:

#import "SegmentControlViewController.h"
@implementation SegmentControlViewController
- (IBAction ) segmentControlAction : ( id )sender
{
UISegmentedControl * control = sender ;
if ( [control selectedSegmentIndex ] == 0 )
{
[ self.view addSubview :firstView ] ;
}
if ( [control selectedSegmentIndex ] == 1 )
{
[ self.view addSubview :secondView ] ;
}
}
- ( void )viewDidLoad
{
firstView.frame = CGRectMake (0, 45, 320, 420 );
secondView.frame = CGRectMake (0, 45, 320, 420 );
[super viewDidLoad ];
[segment addTarget :self action : @selector (segmentControlAction : )
forControlEvents :UIControlEventValueChanged ];
segment.selectedSegmentIndex = 0 ;
}
- ( 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];
}
*/

- ( 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 7: Now compile and run the application on the Simulator.


Source : edumobile[dot]org

0 comments:

Post a Comment