IOS development-custom city selector (province + city + district/county) (storyboard version) and iosstoryboard

Source: Internet
Author: User

IOS development-custom city selector (province + city + district/county) (storyboard version) and iosstoryboard

Step 1: create a single project CitySelectedDemo

Step 2: import the resource area. plist (check the copy option and add the area. plist file resource)

Step 3: Design mian. storyboard

-- Drag the UITextField control (click this input box to display the selector. Select the desired City address and the result is displayed in the input box );

-- Drag the Toolbar Control and UIPickerView control to form a city selector. (rename the Item of the Toolbar Control as "finished". In the future, click "finish" to select the end address, if the "finish" button feels awkward on the left side of the Toolbar, you can drag another Flexibel control on the left side of the "finish" button .)

Step 4: connect

-- Click the first yellow circle at the top of the page, as shown in the following figure:

 

-- Click the blue pattern in the upper right corner, and drag and drop the link from the circle on the Right of New Referencing Outlets under Referencing Outlets to the UIPickerView control to select delegate and dataSource respectively. The result is as follows:

-- Connect the view to ViewController. m

(1) UITextFiled connects a UIOutlet named cityField and an Action named CityAction (select Edit Did Begin for Event connection, which indicates the Action to be executed when editing the input box)

(2) The Toolbar connects to a UIOutlet named cityToolbar, and the "finish" button on the Toolbar connects to an Action named selectedAction.

(3) UIPickerView connects to a UIOutlet and is named cityPicker.

-- Check the den attribute of the Toolbar and UIPickerView controls to make them invisible;

 

Step 5: Encoding

-- Compile the data model HZLocation (new File is required to inherit the NSObject class)

 

#import <Foundation/Foundation.h>@interface HZLocation : NSObject@property (copy, nonatomic) NSString *country;@property (copy, nonatomic) NSString *state;@property (copy, nonatomic) NSString *city;@property (copy, nonatomic) NSString *district;@property (copy, nonatomic) NSString *street;@property (nonatomic) double latitude;@property (nonatomic) double longitude;@end
#import "HZLocation.h"@implementation HZLocation@synthesize country = _country;@synthesize state = _state;@synthesize city = _city;@synthesize district = _district;@synthesize street = _street;@synthesize latitude = _latitude;@synthesize longitude = _longitude;@end

 

 

-- Introduce HZLocation. h In ViewController. h and UIPickerViewDelegate and UIPickerViewDatasource.

#import <UIKit/UIKit.h>#import "HZLocation.h"@interface ViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>@property (strong, nonatomic) HZLocation *locate;@end

 

-- In ViewController. m

#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UITextField *cityField;- (IBAction)CityAction:(id)sender;@property (weak, nonatomic) IBOutlet UIToolbar *cityToolbar;- (IBAction)selectedAction:(id)sender;@property (weak, nonatomic) IBOutlet UIPickerView *cityPicker;@property (nonatomic, strong) NSArray *provinces;@property (nonatomic, strong) NSArray *cities;@property (nonatomic, strong) NSArray *areas;@property (nonatomic, strong) NSString *selected;@end@implementation ViewController@synthesize provinces, cities, areas;@synthesize locate=_locate;@synthesize cityPicker = _cityPicker;- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        self.cityField.inputView = [[UIView alloc] initWithFrame:CGRectZero];        provinces = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil]];    cities = [[provinces objectAtIndex:0] objectForKey:@"cities"];        self.locate.state = [[provinces objectAtIndex:0] objectForKey:@"state"];    self.locate.city = [[cities objectAtIndex:0] objectForKey:@"city"];        areas = [[cities objectAtIndex:0] objectForKey:@"areas"];    if (areas.count > 0) {        self.locate.district = [areas objectAtIndex:0];    } else{        self.locate.district = @"";    }}-(HZLocation *)locate{    if (_locate == nil) {        _locate = [[HZLocation alloc] init];    }        return _locate;}- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{    return 3;}- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{    switch (component) {        case 0:            return [provinces count];            break;        case 1:            return [cities count];            break;        case 2:            return [areas count];            break;                    default:            return 0;            break;    }    }- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{        switch (component) {        case 0:            return [[provinces objectAtIndex:row] objectForKey:@"state"];            break;        case 1:            return [[cities objectAtIndex:row] objectForKey:@"city"];            break;        case 2:            if ([areas count] > 0) {                return [areas objectAtIndex:row];                break;            }        default:            return  @"";            break;    }}- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{    switch (component) {        case 0:            cities = [[provinces objectAtIndex:row] objectForKey:@"cities"];            [self.cityPicker selectRow:0 inComponent:1 animated:YES];            [self.cityPicker reloadComponent:1];                        areas = [[cities objectAtIndex:0] objectForKey:@"areas"];            [self.cityPicker selectRow:0 inComponent:2 animated:YES];            [self.cityPicker reloadComponent:2];                        self.locate.state = [[provinces objectAtIndex:row] objectForKey:@"state"];            self.locate.city = [[cities objectAtIndex:0] objectForKey:@"city"];            if ([areas count] > 0) {                self.locate.district = [areas objectAtIndex:0];            } else{                self.locate.district = @"";            }            break;        case 1:            areas = [[cities objectAtIndex:row] objectForKey:@"areas"];            [self.cityPicker selectRow:0 inComponent:2 animated:YES];            [self.cityPicker reloadComponent:2];                        self.locate.city = [[cities objectAtIndex:row] objectForKey:@"city"];            if ([areas count] > 0) {                self.locate.district = [areas objectAtIndex:0];            } else{                self.locate.district = @"";            }            break;        case 2:            if ([areas count] > 0) {                self.locate.district = [areas objectAtIndex:row];            } else{                self.locate.district = @"";            }            break;        default:            break;    }    NSString *str = [self.locate.state stringByAppendingString:self.locate.city];    _selected = [str stringByAppendingString:self.locate.district];}- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{    UILabel* pickerLabel = (UILabel*)view;    if (!pickerLabel){        pickerLabel = [[UILabel alloc] init];        pickerLabel.adjustsFontSizeToFitWidth = YES;        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];    }    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];    return pickerLabel;}- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{    if (component == 0) {        return 80;    }    if (component == 1) {        return 100;    }    return 120;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (IBAction)CityAction:(id)sender {    self.cityToolbar.hidden = NO;    self.cityPicker.hidden = NO;}- (IBAction)selectedAction:(id)sender {    self.cityToolbar.hidden = YES;    self.cityPicker.hidden = YES;    self.cityField.text = _selected;}@end

 

As follows:

 

 

 

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.