首页 > 代码库 > 普通选择器<数据源协议,委托协议>(IOS开发)

普通选择器<数据源协议,委托协议>(IOS开发)

-普通选择器必须满足这两个协议,一个为委托协议,一个为数据源协议

-委托协议负责控制控件UI、事件响应,实现可选

-数据源协议负责控件与应用数据模型的桥梁,一般必须实现


@interface ViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>



//
//  ViewController.h
//  PickViewSample
//
//  Created by 李亚坤 on 14-10-21.
//  Copyright (c) 2014年 李亚坤. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource>
// 普通选择器必须满足这两个协议,一个为委托协议,一个为数据源协议
// 委托协议负责控制控件UI、事件响应, 实现可选
// 数据源协议负责控件与应用数据模型的桥梁,一般必须实现
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;

@property (weak, nonatomic) IBOutlet UILabel *label;


@property (nonatomic, strong) NSDictionary *pickerData;     // 保存全部数据
@property (nonatomic, strong) NSArray *pickerProvincesData; // 当前省数据
@property (nonatomic, strong) NSArray *pickerCitiesData;    // 当前省下面的市数据


- (IBAction)onclick:(id)sender;
@end


//
//  ViewController.m
//  PickViewSample
//
//  Created by 李亚坤 on 14-10-21.
//  Copyright (c) 2014年 李亚坤. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // 找到目录指定文件并返回文件路径
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *plistPath = [bundle pathForResource:@"provinces_cities"
                                           ofType:@"plist"];
    
    // 获取属性列表文件中的全部数据
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile: plistPath];
    self.pickerData = http://www.mamicode.com/dict;>
    self.pickView.dataSource = self;
    self.pickView.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)onclick:(id)sender {
    // 设置选定的行索引从0开始
    NSInteger row1 = [self.pickerView selectedRowInComponent:0];
    NSInteger row2 = [self.pickerView selectedRowInComponent:1];
    NSString *selected1 = [self.pickerProvincesData objectAtIndex:row1];
    NSString *selected2 = [self.pickerCitiesData objectAtIndex:row2];
    
    NSString *title = [[NSString alloc] initWithFormat:@"%@, %@市", selected1, selected2];
    self.label.text = title;
}

#pragma mark 实现协议UIpickerViewDataSource方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    // 返回选择器中拨轮的数目
    return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    // 返回选择其中某个拨轮的行数
    if (component == 0) {
        // 返回省的个数
        return [self.pickerProvincesData count];
    }
    else
    {
        // 返回市的个数
        return [self.pickerCitiesData count];
    }
}

#pragma mark 实现协议UIPickerViewDelegate方法
// 为选择器中某个拨轮的行提供显示数据
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) {
        // 选择省的名
        return [self.pickerProvincesData objectAtIndex:row];
    }
    else
    {
        return [self.pickerCitiesData objectAtIndex:row];
    }
}
// 当选中某个拨轮中的行时,调用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        NSString *seletedProvince = [self.pickerProvincesData objectAtIndex:row];
        NSArray *array = [self.pickerData objectForKey:seletedProvince];
        self.pickerCitiesData = http://www.mamicode.com/array;>

关于委托和数据源的实现对象ViewController分配给UIPickerView的委托属性Delegate和数据源属性dataSource;

这一部分可以在InterfaceBuilder里面实现,分配会自动生成

-(void) viewDidLoad

{

...


self.pickerView.dataSource = self;

self.pickerView.delegate = self;

}

普通选择器<数据源协议,委托协议>(IOS开发)