In this example we will see how to display DatePicker view after pressing button. So let see how it will worked.
Step 1: Open the Xcode, Create a new project using View Base application. Give the application “Button_Fun”.
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 ViewController class for you. Expand Resources and notice
the template generated a separate nib, Button_FunViewController.xib for the Button_Fun.
Step 4: We need to add one UIViewController class in the application. So select New File -> Cocoa Touch ->
UIViewController subclass, and give the class name “Date Picker”.
Step 5: Open the Button_FunViewController.h file and make the following changes in the file:
@class DatePicker;
@interface Button_FunViewController : UIViewController {
DatePicker *datePicker;
IBOutlet UIButton *button;
}
@property (nonatomic, retain) IBOutlet UIButton *button;
–(IBAction) nextview:(id)sender;
@end
Step 6: Double click the Button_FunViewController.xib file and open it to the Interface Builder. Drag the Round Rect
button from the library and place it to the view window. Select the button and bring up Connection Inspector connect Touch Up Inside to the File’s owner icon and select nextview: method. Now save the .xib file , close it and go back to the Xcode.
Step 7: Open the Button_FunViewController.m file and make the following changes in the file:
#import "DatePicker.h"
@implementation Button_FunViewController
@synthesize button;
–(IBAction) nextview:(id)sender
{
datePicker = [[DatePicker alloc]
initWithNibName:@"DatePicker"
bundle:nil];
[self.view addSubview:datePicker.view];
}
– (void)dealloc
{
[super dealloc];
[datePicker release];
}
– (void)didReceiveMemoryWarning
{
// Releases the view if it doesn’t have a superview.
[super didReceiveMemoryWarning];
}
– (void)viewDidUnload
{
[super viewDidUnload];
}
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
Step 8: Open the DatePicker.h file and make the following change:
@interface DatePicker : UIViewController {
IBOutlet UIDatePicker *datePicker;
}
–(IBAction) previousview:(id)sender;
@end
Step 9: Double click the DatePicker.xib file and open it to the Interface Builder. First drag the date picker from library and
place it to the view window. Connect File’s Owner icon to the DatePicker and select datePicker. Now drag the Round Rect
Button and place it to the view window. Select the button and bring up connection inspector and connect Touch Up Inside to the File’s owner icon and select previousview: method. Now save the .xib file, close it and go back to the Xcode.
Step 10: Open the DatePicker.m file and make the following changes:
@implementation DatePicker
–(IBAction) previousview:(id)sender;
{
[self.view removeFromSuperview];
}
– (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
– (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
– (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
– (void)viewDidUnload
{
[super viewDidUnload];
}
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
Step 11: Now Compile and Run the application on the Simulator.
You can Download SourceCode from Button_Fun
