国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

IOS開發之自制城市選擇器(省份+城市+區/縣城)(storyboard版)

2019-11-14 18:52:52
字體:
來源:轉載
供稿:網友

第一步:新建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

 

效果圖如下:

 

 

 

        


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 岳西县| 金昌市| 太白县| 清水县| 永春县| 永登县| 乐业县| 浦县| 兰考县| 石门县| 道孚县| 城口县| 张家港市| 长乐市| 浦江县| 隆化县| 建宁县| 潮安县| 成安县| 晋江市| 襄城县| 长武县| 聂拉木县| 黄梅县| 三台县| 凤庆县| 通江县| 武夷山市| 南康市| 武乡县| 桃源县| 出国| 资源县| 高邑县| 汉源县| 宜君县| 清镇市| 斗六市| 土默特左旗| 江西省| 长岛县|