In iOS, add a UIPickerView instance to UIAcitionSheet.

Source: Internet
Author: User

In iOS, add a UIPickerView instance to UIAcitionSheet.
Before writing, we can't control some things, but at least we can try to make it better. I once said this, I may not be the best, but what I give you is definitely the best. Find what you want and live your life. Don't look back even if it's hard. A person has at least one dream. There is a reason to be strong. If his heart is not a place to live, he will be wandering everywhere.
-- J! Nl! N

I hope everyone is happy and happy.

As required by the project, the date selector and the region linkage selector are involved. Considering that the use of open-source library resources is not particularly cost-effective, I decided to rewrite the controls of the iOS system. In the true sense, it is not actually a rewrite. It is only used to fill in UIPickerFView when adding some la s in UIAcitionSheet. The code is relatively streamlined, But it seems bloated in some places. It will be further optimized over time and with the accumulation of technology. This log is only recorded and will be updated and collected by the blog.

First, let's take a look at the following:

Here, we use the built-in UIActionSheet, which should be known to anyone familiar with iOS development. UIActionSheet is a selection button control popped up at the bottom. You can add multiple items and add click events for each item. Its initialization code is:

 

NSString * title = UIDeviceOrientationIsLandscape ([UIDevice currentDevice]. orientation )? @; UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: title delegate: self cancelButtonTitle: @ cancel destructiveButtonTitle: nil otherButtonTitles: @ OK, nil];
We can see that an NSString is initialized here, and then the Padding string is specified using the three-object Operator Based on the screen landscape. Here, we only use the line break as the character to make an empty layout at the top of the UIActionSheet for UIDatePicker and UIPickerView.

 

 

actionSheet.userInteractionEnabled = YES;actionSheet.backgroundColor = [UIColor clearColor];datePicker = [[UIDatePicker alloc] init];datePicker.tag = 1000;datePicker.datePickerMode = UIDatePickerModeDate;[actionSheet addSubview:datePicker];[actionSheet showInView:self.view];actionSheet.tag = 100;
The preceding Code sets UIActionSheet to interactive. The background is white and the background color must be set. Otherwise, some models may display characters. Then initialize UIDatePicker and bind the tag to provide subsequent method calls. Then set the date format, which can also be the date and time format. We recommend that you use the addSubView method of UIActionSheet to add it to the layout where we previously used a line break placeholder, and use showInView to display it at the top, and set a tag value tag for UIActionSheet.

 


The preceding figure shows how to add a date selector.
Similarly, regional linkages are the same. But it is relatively complicated. I will not go into details here. Directly paste the implementation part.

The plist file is the information of common provinces, municipalities, and counties in China. Currently, it is not perfect. I have limited geographical knowledge and need to optimize and add it.
# Pragma mark-call the region linkage method-(void) showAreaPickerView {// load the plist file, initialize three NSArray objects, and then perform some illegal operations, you know provinces = [[NSArray alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ area. plist ofType: nil]; cities = [[provinces objectAtIndex: 0] objectForKey: @ cities]; state = [[provinces objectAtIndex: 0] objectForKey: @ state]; NSString * title = UIDeviceOrientationIsLandscape ([UIDevice currentDevi Ce]. orientation )? @: @; UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: title delegate: self cancelButtonTitle: @ cancel destructiveButtonTitle: nil accept: @ OK, nil]; actionSheet. userInteractionEnabled = YES; actionSheet. backgroundColor = [UIColor clearColor]; areaPicker = [[UIPickerView alloc] init]; areaPicker. dataSource = self; areaPicker. delegate = self; [actionSheet addSubview: areaPicker]; [actionSheet showInView: self. view]; actionSheet. tag = 200 ;}
The following describes the UIPickerViewDataSource data source method and UIPickerViewDelegate proxy method. Some areas can still be simplified.
// The method defined in UIPickerViewDataSource. the return value of this method determines how many columns the control contains-(NSInteger) numberOfComponentsInPickerView :( UIPickerView *) pickerView {return 3; // What does return 3 indicate? If you do not know this, don't do it. It indicates that the control only contains three columns, and the three columns are enough.} // the method defined in UIPickerViewDataSource, the return value of this method determines the number of items in the list containing the specified columns-(NSInteger) pickerView :( UIPickerView *) pickerView numberOfRowsInComponent :( NSInteger) component {// if it is the first column, returns the number of provinces. // that is, the number of elements contained in provinces. if (component = 0) {return provinces. count;} else if (component = 1) {// if it is the second column, return the number of cities return cities. count;} else {return areas. count ;}}// method defined in UIPickerViewDelegate. This method returns NSString as the title (NSString *) pickerView :( UIPickerView *) displayed on the specified column and list item in UIPickerView *) pickerView titleForRow :( NSInteger) row forComponent :( NSInteger) component {// if it is the first column, return the switch (component) {case 0: return [[provinces objectAtIndex: row] objectForKey: @ state]; break; case 1: return [[cities objectAtIndex: row] objectForKey: @ city]; break; case 2: return [areas objectAtIndex: row]; break; default: return @; break; }}// this method is triggered when the user selects a specified column and list item in UIPickerViewDataSource-(void) pickerView :( UIPickerView *) pickerView didSelectRow :( NSInteger) row inComponent :( NSInteger) component {switch (component) {case 0: cities = [[provinces objectAtIndex: row] objectForKey: @ cities]; [self. areaPicker selectRow: 0 inComponent: 1 animated: YES]; [self. areaPicker reloadComponent: 1]; areas = [[cities objectAtIndex: 0] objectForKey: @ areas]; [self. areaPicker selectRow: 0 inComponent: 2 animated: YES]; [self. areaPicker reloadComponent: 2]; state = [[provinces objectAtIndex: row] objectForKey: @ state]; city = [[cities objectAtIndex: 0] objectForKey: @ city]; if ([areas count]> 0) {district = [areas objectAtIndex: 0];} else {district =@;} break; case 1: areas = [[cities objectAtIndex: row] objectForKey: @ areas]; [self. areaPicker selectRow: 0 inComponent: 2 animated: YES]; [self. areaPicker reloadComponent: 2]; city = [[cities objectAtIndex: row] objectForKey: @ city]; if ([areas count]> 0) {district = [areas objectAtIndex: 0];} else {district = @;} break; case 2: if ([areas count]> 0) {district = [areas objectAtIndex: row];} else {district =@;} break ;}}
The specific logic implementation may be difficult to handle with some small details, but the actual use is still great. Dynamic gif images are not pasted here. This article ends.



 

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.