IOS开发之自制城市选择器(省份+城市+区/县城)(storyboard版)
第一步:新建single工程CitySelectedDemo
第二步:导入资源area.plist(千万勾选copy选项,后面附area.plist文件资源)
第三步:设计mian.storyboard
——》拖拽UITextField控件(运行后点击此输入框会弹出选择器,选择我们想要的城市地址后结果显示在输入框中);
——》拖拽Toolbar控件和UIPickerView控件组成城市选择器;(将Toolbar控件的Item更名为“完成”,将来点击“完成”按钮结束地址的选择,如果“完成”按钮在Toolbar的左侧觉得别扭可再拖拽一个Flexibel控件于“完成”的左侧)结果如图:
第四步:连线
——》点击上图最上面的第一个黄色圆形图案,出现如下图界面:
——》点击上图右上角显示蓝色的图案;再从下方Referencing Outlets下得New Referencing Outlets右侧的圆圈中拖拽连线到UIPickerView控件上分别选择delegate和dataSource;结果如下图:
——》再连线视图与ViewController.m
(1)UITextFiled连接一个UIOutlet命名为cityField和一个Action命名为CityAction(Action连接上Event选择Edit Did Begin,表示开始编辑输入框时就要执行的动作)
(2)Toolbar连接一个UIOutlet命名为cityToolbar,Toolbar上的“完成”按钮连接一个Action命名为selectedAction
(3)UIPickerView连接一个UIOutlet命名为cityPicker
——》将Toolbar与UIPickerView两个控件的Hidden属性勾选, 使其不可见;
第五步:编码
——》编写数据模型HZLocation(需新建File,继承NSObject大类)
#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
——》在ViewController.h中,引入HZLocation.h,并引入UIPickerViewDelegate,UIPickerViewDatasource
#import <UIKit/UIKit.h> #import "HZLocation.h" @interface ViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource> @property (strong, nonatomic) HZLocation *locate; @end
——》在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
效果图如下:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。