Drop down lists for iPhone: rolling your own
2013-01-29 09:33
423 查看
转:http://t2a.co/blog/index.php/drop-down-lists-for-iphone-rolling-your-own/
If like me, you’re fairly new to iPhone development, you might be wondering how to implement drop down lists in your app. The fact of the matter is that there is no native control provided by Apple, similar to the <select> tag
you’ll know well if you have a background in web development. So, the solution is to roll our own: stick with me for a few minutes – I promise it’s not as tricky as it sounds. Here is how our finished control will look:
We’re going to use a UIButton control with a custom background image and put a label on top of it to show the currently selected option. Go ahead
and add a UIButton and a UILabelto
your view using the Interface Builder. If you’re not much of a designer, leave the background image for now and just use a regular button – it doesn’t really matter to get the code working as we need it.
Add an IBOutlet for your label to your view controller’s header file and implement theUIPickerViewDelegate and UIPickerViewDataSource delegates.
We also need to define an NSMutableArray, a UIActionSheet and
an NSInteger – We’ll deal with these and the delegates we are implementing in just a second. Lastly, declare an IBActionshowSearchWhereOptions which
we need to connect to our button in the Interface Builder. Our finished header file looks like this:
Next – on to our implementation file. Implement the following methods that are required because of the delegates we have implemented.
Most of the work happens here.. In our showSearchWhereOptions method, which runs when the user taps our “button” (it is a button really, but
it’s sneakily pretending to be a drop down list control).
We haven’t yet defined our list items – we’ll do this in our viewDidLoad method. We need to make sure we set a default search option too. It’s
a zero based index, so setting it to 0 will make “Specified location” our default selection.
One last thing – implement the dismissActionSheet method which is responsible for hiding the action sheet when we click the done button.
If like me, you’re fairly new to iPhone development, you might be wondering how to implement drop down lists in your app. The fact of the matter is that there is no native control provided by Apple, similar to the <select> tag
you’ll know well if you have a background in web development. So, the solution is to roll our own: stick with me for a few minutes – I promise it’s not as tricky as it sounds. Here is how our finished control will look:
We’re going to use a UIButton control with a custom background image and put a label on top of it to show the currently selected option. Go ahead
and add a UIButton and a UILabelto
your view using the Interface Builder. If you’re not much of a designer, leave the background image for now and just use a regular button – it doesn’t really matter to get the code working as we need it.
Add an IBOutlet for your label to your view controller’s header file and implement theUIPickerViewDelegate and UIPickerViewDataSource delegates.
We also need to define an NSMutableArray, a UIActionSheet and
an NSInteger – We’ll deal with these and the delegates we are implementing in just a second. Lastly, declare an IBActionshowSearchWhereOptions which
we need to connect to our button in the Interface Builder. Our finished header file looks like this:
it’s sneakily pretending to be a drop down list control).
a zero based index, so setting it to 0 will make “Specified location” our default selection.
相关文章推荐
- (转)How to Update Your Apps for the 4-Inch iPhone 5 Display
- xcode7的那些坑-“Your binary is not optimized for iPhone 5” (ITMS-90096) when submitting
- How To Choose The Best XML Parser for Your iPhone Project
- Overloading the << Operator for Your Own Classes
- Android.mk for your own module
- xcode7的那些坑-“Your binary is not optimized for iPhone 5” (ITMS-90096) when submitting
- [转]Rolling Your Own - Building IRPs to Perform I/O
- App Store提交 报错 “Your binary is not optimized for iPhone 5” (ITMS-90096) when submitting
- xcode7的那些坑-“Your binary is not optimized for iPhone 5” (ITMS-90096) when submitting
- Android.mk for your own module
- Creating your own ODS Style Template for PDF Output
- Android.mk for your own module
- (转载)XML Tutorial for iOS: How To Choose The Best XML Parser for Your iPhone Project
- Taking Responsibility for Your Own Learning
- 上传iOS应用时 ERROR ITMS-90096: Your binary is not optimized for iPhone 5。。。
- SAP CRM How to Create your own BOL Object for webclient
- Rapid Portlet Development with WebSphere Portlet Factory: Step-by-Step Guide for Building Your Own P
- Your binary is not optimized for iPhone 5” (ITMS-90096) when submitting
- Android.mk for your own module
- Android.mk for your own module