DropDown in IPhone using UITextField,PickerView or DatePickerView

来源:互联网 发布:无网络最火的游戏 编辑:程序博客网 时间:2024/05/21 06:31

From: http://www.nevpro.co.in/blogs/entry/13-dropdown-in-iphone-using-uitextfieldpickerview-or-datepickerview.html

In Iphone xcode we do not have the Dropdown/select object that we can drag into our 
Interface builder.Also if we use the UIPicker/UIDatepicker then it occupies more
space on iPhone screen. So there is alternate solution we have that when we start
editing the text of UITextField there will be pop-up occures of PickerView.
So in this blog we are going to see how to generate dropdown/select in xcode in iphone
in few steps.

Step 1: Open the xcode, create new application using View Base Application and name 
it as 'GetDropdown'.

Step 2: Choose your interface builder. Here i selected Window ('Dropdown.xib').Now 
you will see xcode automatically creates properly the directiory structure for you.

Step 3: Add two UITextFields one is for Simple PickerView and another is for DatePickerView
and add UIPickerView, UIDatePickerView on Interface Builder.

 


Step 4: Add new ViewController and Name it as 'popViewController' or if you have already 
then go to step no.5

Step 5: Now select 'popViewController.h' add the following code:

1.<hi there="there"></hi>
2.#import <thanks></thanks>
3.@interface zodiacViewController : UIViewController
4.@property(nonatomic,weak)IBOutlet UIPickerView *myPicker;
5.@property(nonatomic,weak)IBOutlet UITextField *dropDown;
6.-(IBAction)showPicker:(id)sender;
7.@end


Step 6:Now select 'popViewController.m' add the following code:

01.#import "zodiacViewController.h"
02.#import "secondViewControllr.h"
03.@implementation zodiacViewController
04.@synthesize dropDown;
05.@synthesize myPicker;
06. 
07.//On touch inside the textField this action will be executed
08. 
09.-(IBAction)showPicker:(id)sender
10.{
11.myPicker.showsSelectionIndicator = YES;
12.myPicker.delegate = self;
13.myPicker.dataSource = self;
14. 
15.UIToolbar* toolbar = [[UIToolbar alloc] init];
16.toolbar.barStyle = UIBarStyleBlackTranslucent;
17.[toolbar sizeToFit];
18. 
19.//to make the done button aligned to the right
20.UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc]
21.initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
22. 
23.UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
24.style:UIBarButtonItemStyleDone target:self
25.action:@selector(doneClicked:)];
26. 
27.[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
28. 
29.dropDown.inputView = myPicker; //Set the view source to textField
30.dropDown.inputAccessoryView = toolbar;
31.}


Step 7:Now Inspect the IBOutlet of your 'pop-ViewController.h' with your corresponding objects.
As you two add IBAction in your 'pop-ViewController.h' then inspect those IBAction with
corresponding  UITextField's 'event property 'Editing Did Begin'.

Step 8:Now your application is done. So start compiling the applicaton. and run it.
You will get the following screen.

 

原创粉丝点击