麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

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

2019-11-14 18:52:52
字體:
供稿:網(wǎng)友

第一步:新建single工程CitySelectedDemo

第二步:導(dǎo)入資源area.plist(千萬勾選copy選項,后面附area.plist文件資源)

第三步:設(shè)計mian.storyboard

      ——》拖拽UITextField控件(運行后點擊此輸入框會彈出選擇器,選擇我們想要的城市地址后結(jié)果顯示在輸入框中);

      ——》拖拽Toolbar控件和UipickerView控件組成城市選擇器;(將Toolbar控件的Item更名為“完成”,將來點擊“完成”按鈕結(jié)束地址的選擇,如果“完成”按鈕在Toolbar的左側(cè)覺得別扭可再拖拽一個Flexibel控件于“完成”的左側(cè))結(jié)果如圖:

      

第四步:連線

       ——》點擊上圖最上面的第一個黃色圓形圖案,出現(xiàn)如下圖界面:

 

          ——》點擊上圖右上角顯示藍(lán)色的圖案;再從下方Referencing Outlets下得New Referencing Outlets右側(cè)的圓圈中拖拽連線到UIPickerView控件上分別選擇delegate和dataSource;結(jié)果如下圖:

     

       ——》再連線視圖與ViewController.m

        (1)UITextFiled連接一個UIOutlet命名為cityField和一個Action命名為CityAction(Action連接上Event選擇Edit Did Begin,表示開始編輯輸入框時就要執(zhí)行的動作)

       (2)Toolbar連接一個UIOutlet命名為cityToolbar,Toolbar上的“完成”按鈕連接一個Action命名為selectedAction

       (3)UIPickerView連接一個UIOutlet命名為cityPicker

      ——》將Toolbar與UIPickerView兩個控件的Hidden屬性勾選, 使其不可見;

 

第五步:編碼

     ——》編寫數(shù)據(jù)模型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

 

效果圖如下:

 

 

 

        


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 久草在线视频网 | 欧美一区二区三区免费不卡 | 黄色网址在线免费播放 | 日本成年网 | 久久性生活免费视频 | 日本黄色大片免费 | 午夜男人免费视频 | 国产精品久久av | av国产免费| chinesexxxx刘婷hd| 日韩中文字幕一区二区三区 | 久久久日韩av免费观看下载 | 国产一区二区在线观看视频 | 国产91在线播放九色 | 久久国产精品久久久久久电车 | 草草视频免费观看 | 九九精品在线观看视频 | 精品国产高清一区二区三区 | 国产精品视频久久久 | 中国7777高潮网站 | 国产精品亚洲三区 | 久草手机在线观看视频 | 日韩剧情片 | 亚洲精中文字幕二区三区 | 看免费一级毛片 | 亚洲精品一区国产精品丝瓜 | 久久影院一区二区三区 | 99国产精品白浆在线观看免费 | 欧美女人天堂 | 成人宗合网| 国产精品一区在线免费观看 | 国产精品色在线网站 | 日本在线播放一区二区 | 成人在线视频免费播放 | 色婷婷a v | 毛片在线免费视频 | 国产成人自拍视频在线 | 欧美18一12sex性处hd | 中文字幕在线播放第一页 | 国产九色在线观看 | 亚洲国产精品久久久久制服红楼梦 |