Picker in IOS

Source: Internet
Author: User

In brief, Picker is often used in some settings of the application to help users quickly select values. UIDatePicker, UIPickerView, and UIImagePickerController are usually used. The initial interface is used to complete the selection on the established interface. The first step is to set the attributes of date selection UIDatePicker: localedatePickerMode [cpp] UIDatePicker * datePicker = [[UIDatePicker alloc] init]; datePicker. locale = [[NSLocale alloc] initWithLocaleIdentifier: @ "zh_Hans_cn"]; datePicker. datePickerMode = UIDatePickerModeDate; NSString * stdDate = @ "1990-01-01"; NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; formatter. dateFormat = @ "yyyy-MM-dd"; [datePicker setDate: [Matter dateFromString: stdDate] animated: YES]; [datePicker addTarget: self action: @ selector (showDate :) forControlEvents: UIControlEventValueChanged]; _ dateField. inputView = datePicker; _ dateField. text = stdDate; here, we also set its initial date. Alignment events can be monitored for the purpose, and there is no proxy protocol. [Cpp]-(void) showDate :( UIDatePicker *) datePicker {NSDate * date = datePicker. date; NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; formatter. dateFormat = @ "yyyy-MM-dd"; NSString * outputString = [formatter stringFromDate: date]; _ dateField. text = outputString;} If UIImagePickerController does not customize the photo view, it is very easy to select photos or photos, and you can quickly call it by setting the protocol. [Cpp] UIImagePickerController * imagePC = [[UIImagePickerController alloc] init]; // select an image from the photo library, or set the camera imagePC. sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // allows you to edit the image in a simple way. allowsEditing = YES; imagePC. delegate = self; [self presentViewController: imagePC animated: YES completion: nil]; obtain the image using the Protocol method and set [cpp]-(void) imagePickerController :( UIImagePickerController *) picker didFinishPi CkingMediaWithInfo :( NSDictionary *) info {const NSString * REditedImageIdentifier = @ "Courier"; UIImage * image = info [identifier]; [_ imageButton setImage: image forState: UIControlStateNormal]; [picker dismissViewControllerAnimated: YES completion: nil];} The PickerView is slightly more complex when there are many components. First, initialize and set the attribute [cpp]-(void) pickerViewAndMethod {UIPickerView * pickerView = [[UIPickerView alloc] init]; pickerView. delegate = self; pickerView. dataSource = self; // display the pickerView of the selector. showsSelectionIndicator = YES; _ cityField. inputView = pickerView; _ cityField. text = @ "Beijing-Haidian";} implements data sources and proxy methods to refresh data and interfaces. [Cpp]-(NSInteger) numberOfComponentsInPickerView :( UIPickerView *) pickerView {return rComponentCount;}-(NSInteger) pickerView :( UIPickerView *) pickerView numberOfRowsInComponent :( NSInteger) component {if (component = rProvinceComponent) {return _ cityList. count;} else {NSInteger I = [pickerView selectedRowInComponent: rProvinceComponent]; NSArray * arr = _ cityList [I] [@ "city"]; return arr. count; }-(NSString *) pickerView :( UIPickerView *) pickerView titleForRow :( NSInteger) row forComponent :( NSInteger) component {if (component = rProvinceComponent) {NSString * proName = _ cityList [row] [@ "province"]; return proName;} else {NSInteger I = [pickerView selectedRowInComponent: rProvinceComponent]; NSString * cityName = _ cityList [I] [@ "city"] [row]; return cityName ;}- (void) pickerView :( UIPicke RView *) pickerView didSelectRow :( NSInteger) row inComponent :( NSInteger) component {[pickerView reloadComponent: rCityComponent]; NSInteger proIndex = [pickerView selectedRowInComponent: rProvinceComponent]; NSString * proName = _ cityList [proIndex] [@ "province"]; NSInteger cityIndex = [pickerView selectedRowInComponent: rCityComponent]; NSString * cityName = _ cityList [proIndex] [@ "city"] [cityIndex]; _ cityF Ield. text = [NSString stringWithFormat: @ "% @-% @", proName, cityName];} the data source is a simple Plist file written by yourself. An array contains multiple dictionaries. In this way, you can use various Picker settings.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.