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

 

效果圖如下:

 

 

 

        


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM