How to Create Combobox control in iPhone ?
iPhone does not provide direct combobox control like in android for creating combobox in iPhone we have to use UIPicker
control to selecting combo item from picker view.
control to selecting combo item from picker view.
1.Create View-Base Application right click and create one new group and give name images and put image of drop down arrow
image download from here. Now open your ComboBoxDemoViewController.xib file create one UITextField and UIButton and
set UIButton background property to image name and adjust UITextField and UIButton like a Combobox.
2.Now Modify ComboBoxDemoViewController.h file define IBOutlet and IBAction for UITextField and UIButton and create
UIActionSheet,NSDate and UIDatePicker.
#import <UIKit/UIKit.h>
@interface ComboBoxViewController : UIViewController <UIActionSheetDelegate>
{
IBOutlet UITextField * combobox;
IBOutlet UIActionSheet * obj_actionSheet;
NSDate * mydate;
UIDatePicker * datePicker;
UIDatePicker * tempPicker;
}
-(IBAction)selectComboItem:(id)sender;
@end
3. Now set IBOutlet to your UITextField and IBAction to UIButton touch up inside event and click on UITextField and set property
in Attribute Inspector pan set editable check box uncheck.
4.Now modify your ComboBoxViewController.m file as following and build your project and run on simulator.
#import "ComboBoxViewController.h"
#define datePickerTag 100
@implementation ComboBoxViewController
-(IBAction)selectComboItem:(id)sender
{
obj_actionSheet=[[UIActionSheet alloc]initWithTitle:@"" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Done" otherButtonTitles:nil, nil];
[obj_actionSheet showInView:self.view];
[obj_actionSheet setFrame:CGRectMake(0,70,320,400)];
[obj_actionSheet release];
}
-(void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
datePicker=[[UIDatePicker alloc]initWithFrame:CGRectMake(0, 30,300,400)];
datePicker.datePickerMode=UIDatePickerModeDate;
datePicker.minimumDate=[NSDate date];
[actionSheet addSubview:datePicker];
NSArray * actionSubView=[actionSheet subviews];
[[actionSubView objectAtIndex:1]setFrame:CGRectMake(20, 250, 280, 46)];
[[actionSubView objectAtIndex:2]setFrame:CGRectMake(20, 300, 280, 46)];
[datePicker setTag:datePickerTag];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != [actionSheet cancelButtonIndex])
{
NSDateFormatter * dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
tempPicker=(UIDatePicker *)[actionSheet viewWithTag:datePickerTag];
mydate=[tempPicker date];
NSString * dateString=[dateFormatter stringFromDate:mydate];
combobox.text=dateString;
[dateFormatter release];
}
else
{
combobox.text=@"";
}
}
- (void)dealloc
{
[super dealloc];
}
#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
image download from here. Now open your ComboBoxDemoViewController.xib file create one UITextField and UIButton and
set UIButton background property to image name and adjust UITextField and UIButton like a Combobox.
2.Now Modify ComboBoxDemoViewController.h file define IBOutlet and IBAction for UITextField and UIButton and create
UIActionSheet,NSDate and UIDatePicker.
#import <UIKit/UIKit.h>
@interface ComboBoxViewController : UIViewController <UIActionSheetDelegate>
{
IBOutlet UITextField * combobox;
IBOutlet UIActionSheet * obj_actionSheet;
NSDate * mydate;
UIDatePicker * datePicker;
UIDatePicker * tempPicker;
}
-(IBAction)selectComboItem:(id)sender;
@end
3. Now set IBOutlet to your UITextField and IBAction to UIButton touch up inside event and click on UITextField and set property
in Attribute Inspector pan set editable check box uncheck.
4.Now modify your ComboBoxViewController.m file as following and build your project and run on simulator.
#import "ComboBoxViewController.h"
#define datePickerTag 100
@implementation ComboBoxViewController
-(IBAction)selectComboItem:(id)sender
{
obj_actionSheet=[[UIActionSheet alloc]initWithTitle:@"" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Done" otherButtonTitles:nil, nil];
[obj_actionSheet showInView:self.view];
[obj_actionSheet setFrame:CGRectMake(0,70,320,400)];
[obj_actionSheet release];
}
-(void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
datePicker=[[UIDatePicker alloc]initWithFrame:CGRectMake(0, 30,300,400)];
datePicker.datePickerMode=UIDatePickerModeDate;
datePicker.minimumDate=[NSDate date];
[actionSheet addSubview:datePicker];
NSArray * actionSubView=[actionSheet subviews];
[[actionSubView objectAtIndex:1]setFrame:CGRectMake(20, 250, 280, 46)];
[[actionSubView objectAtIndex:2]setFrame:CGRectMake(20, 300, 280, 46)];
[datePicker setTag:datePickerTag];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != [actionSheet cancelButtonIndex])
{
NSDateFormatter * dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
tempPicker=(UIDatePicker *)[actionSheet viewWithTag:datePickerTag];
mydate=[tempPicker date];
NSString * dateString=[dateFormatter stringFromDate:mydate];
combobox.text=dateString;
[dateFormatter release];
}
else
{
combobox.text=@"";
}
}
- (void)dealloc
{
[super dealloc];
}
#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
Releted Posts :
1. UIText Field with default and number pad keybord
2. Animation like a toggle button in iphone
3. UIActionSheet button event in iphone
4. UISegmented control in iPhone with value change event
5. Create simple UINavigation controller in iPhone
2. Animation like a toggle button in iphone
3. UIActionSheet button event in iphone
4. UISegmented control in iPhone with value change event
5. Create simple UINavigation controller in iPhone
HTML Comment Box is loading comments...