A few days ago, I encountered the situation of selecting data when WRITING the project, so I encapsulated the following three selectors (single-row data selection, date selection and city selection), which were modeled to the controller in the way of AlterController. The principle is the same as PickerView. City selection involves three-level linkage, and the middle number of three-level linkage is also written Group transgressions (which are easier to avoid if you use a model). Welcome interested students can contact me at [email protected] to discuss with me

Github.Demo address: github.com/TynnPassBy/…

Download and drag the project to import a header file:

#import "AlterController.h"
Copy the code

I. Single line selection effect:

Single line effect implementation code:

1. Create an AlterController to be ejected

AlterController *pickerView = [[AlterController alloc]initWithTitle:@"Select single row data" type:kTypeOfSingleLine]; // Set data pickerview. dataArray = @[@"Hatano Knot coat"The @"Sky Sea Wings"The @Maria OzawaThe @"The bridge has not been long."]; // Set the pickerView.delegate = self; [self presentViewController:pickerView animated:YES completion:nil]; SelectedBtn = sender; selectedBtn = sender; selectedBtn = sender; selectedBtn = sender;Copy the code

2. Implement proxy methods

-(void)getResultFromPickView:(NSString *)result{
    [self.selectedBtn setTitle:result forState:UIControlStateNormal];
}
Copy the code

Tip:

  • When creating AlterController, enter the type as required. The type value varies according to data

Ii. Date selection effect:

Date selection code:

1. Create an AlterController to be ejected

AlterController *pickerView = [[AlterController alloc]initWithTitle:@"Select date" type:kTypeOfDate]; pickerView.dataArray = [self getDateData]; pickerView.delegate = self; [self presentViewController:pickerView animated:YES completion:nil]; // mark the selected button self.selectedBtn = sender;Copy the code

2. Load date data

- (NSArray *)getDateData{NSInteger year = 1900; NSMutableArray *yearArray = [NSMutableArray array];for (NSInteger i = 0; i<130; i++) {
        NSString *newYear = [NSString stringWithFormat:@"%zd",year + i];
        [yearArray addObject:newYear];
    }
    NSInteger month = 0;
    NSMutableArray *monthArray = [NSMutableArray array];
    for (NSInteger i = 0; i<12; i++) {
        NSString *newMonth = [NSString stringWithFormat:@"%zd",month + i + 1];
        [monthArray addObject:newMonth];
    }
    NSMutableArray *totalArray = [NSMutableArray array];
    [totalArray addObject:yearArray];
    [totalArray addObject:monthArray];
    return totalArray;
}
Copy the code

3. Implement proxy methods

-(void)getResultFromPickView:(NSString *)result{
    [self.selectedBtn setTitle:result forState:UIControlStateNormal];
}
Copy the code

3. City selection effect:

City Selection Code:

1. Create an AlterController to be ejected

AlterController *pickerView = [[AlterController alloc]initWithTitle:@"Choose a City" type:kTypeOfCity]; pickerView.dataArray = [self getCityData]; pickerView.delegate = self; [self presentViewController:pickerView animated:YES completion:nil]; // mark the selected button self.selectedBtn = sender;Copy the code

2. Load city data

Json - (NSArray *)getCityData{//province. Json - (NSArray *)getCityData NSString* path = [[NSBundle mainBundle] pathForResource:@"province" ofType:@"json"];
    NSString* cityNames = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    if (cityNames == nil) {
        NSLog(@"Error: loading city data is empty");
        return nil;
    }
    NSData *jsonData = [cityNames dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSArray *arrayCityNames = [NSJSONSerialization JSONObjectWithData:jsonData
                                                   options:NSJSONReadingMutableContainers
                                                     error:&err];
    if(err) {
        NSLog(@"Json parsing failed: %@",err);
        return nil;
    }
    return arrayCityNames;
}
Copy the code

3. Implement proxy methods

-(void)getResultFromPickView:(NSString *)result{
    [self.selectedBtn setTitle:result forState:UIControlStateNormal];
}
Copy the code

4. To summarize

  • Date selection,DateFormatter will be more complete, you can pay attention to it, click a focus, thank you
  • Recommended reference articles: www.jianshu.com/p/578065eab…