一般セレクタ(IOS開発)
4220 ワード
-通常のセレクタは、委任プロトコルとデータ・ソース・プロトコルの2つのプロトコルを満たす必要があります.
-委任プロトコルはコントロールUI、イベント応答を制御し、オプションを実現する
-データソースプロトコルはコントロールとデータモデルの適用の橋渡しを担当し、一般的に実現しなければならない.
@interface ViewController : UIViewController
委託とデータソースの実装オブジェクトViewControlがUIPCkerViewに割り当てた委託属性Delegateとデータソース属性dataSourceについて.
この部分はInterfaceBuilderで実現でき、割り当ては自動的に生成されます
-(void) viewDidLoad
{
...
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
}
-委任プロトコルはコントロールUI、イベント応答を制御し、オプションを実現する
-データソースプロトコルはコントロールとデータモデルの適用の橋渡しを担当し、一般的に実現しなければならない.
@interface ViewController : UIViewController
//
// ViewController.h
// PickViewSample
//
// Created by on 14-10-21.
// Copyright (c) 2014 . All rights reserved.
//
#import
@interface ViewController : UIViewController
// , ,
// 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 = dict;
//
self.pickerProvincesData = [self.pickerData allKeys];
//
NSString *seletedProvince = [self.pickerProvincesData objectAtIndex: 0];
// objectforkey:
self.pickerCitiesData = [self.pickerData objectForKey:seletedProvince];
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 = array;
//
[self.pickerView reloadComponent:1];
}
}
@end
委託とデータソースの実装オブジェクトViewControlがUIPCkerViewに割り当てた委託属性Delegateとデータソース属性dataSourceについて.
この部分はInterfaceBuilderで実現でき、割り当ては自動的に生成されます
-(void) viewDidLoad
{
...
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
}