DropDown in IPhone using UITextField,PickerView or DatePickerView

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.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值