IOS----委托协议与数据源协议

 1、介绍

对象之间需要相互通信,一种名叫“委托模式”的编程设计模式来实现对象之间的通信。

该模式主旨是:定义一套接口,某对象若想接受另一个对象的委托,则需要遵从此接口,以便成为其“委托对象”(delegate)。而这“另一个对象”则可以给其委托对象回传一些信息,也可以在发生相关事件时通知委托对象。

该模式可以将数据与业务逻辑解耦。比如一个显示一系列数据所用的视图,这个视图对象的属性中,可以包含负责数据与事件处理的对象,这两种对象分别称为“数据源”(data source)与“委托”(delegate)。

实现委托对象:声明某个类遵从委托协议,然后把协议中想实现的方法在类中实现出来。某个类若遵从委托协议,可以在接口中声明,也可以在class-continuation分类中声明,一般都是在class-continuation分类里声明。

当某对象需要从另外一个对象中获取数据时,可以使用委托模式,在这种情境下,该模式也可以称为“数据源协议”(data source protocal)。

2、选择器 DataPicher

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    [self.myDatapick addTarget:self action:@selector(onDataChange:) forControlEvents: UIControlEventValueChanged];

}

-(IBAction)onDataChange:(UIDatePicker *)dataPicker {

    NSData * data = self.myDatapick.date;

    

    NSDateFormatter * dataFormatter=[[NSDateFormatter alloc] init];

    dataFormatter.dateFormat=@"YYYY-MM-dd HH:mm:ss";   //设置时间格式

    NSString *dateString = [dataFormatter stringFromDate:data];

    NSLog(@"formate date is:%@", dateString);

    self.timeLabel.text = dateString;  //label显示时间

    

}

@end

3、普通选择器 UIPicherView

3.1 创建plist资源文件或者导入plist文件

创建:New files > Resource > Property List 命名为provinces

创建时候注意上图的类型:dictionary、array等

3.2源码

上图三个控件:label、button、普通选择器

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

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

@property (weak, nonatomic) IBOutlet UIDatePicker *myDatapick;

@property (weak, nonatomic) IBOutlet UIPickerView *picjView;

@property (weak, nonatomic) IBOutlet UIButton *button;

@end

#import "ViewController.h"

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property(strong,nonatomic) NSDictionary *allData;

@property(strong,nonatomic) NSArray *level1;

@property(strong,nonatomic) NSArray *level2;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    [self.myDatapick addTarget:self action:@selector(onDataChange:) forControlEvents: UIControlEventValueChanged];

    //委托协议和数据源分配

    self.picjView.dataSource=self;

    self.picjView.delegate=self;

    

    [self initData];

    [self.button addTarget:self action:@selector(onClickButton:) forControlEvents:(UIControlEventTouchDown)];

}

-(IBAction)onDataChange:(UIDatePicker *)dataPicker {

    NSData * data = self.myDatapick.date;

    

    NSDateFormatter * dataFormatter=[[NSDateFormatter alloc] init];

    dataFormatter.dateFormat=@"YYYY-MM-dd HH:mm:ss";

    NSString *dateString = [dataFormatter stringFromDate:data];

    NSLog(@"formate date is:%@", dateString);

    self.timeLabel.text = dateString;

    

}

-(IBAction)onClickButton:(id)sender{

    NSInteger row1=[self.picjView selectedRowInComponent:0];

    NSInteger row2=[self.picjView selectedRowInComponent:1];

    NSString *selected1=[self.level1 objectAtIndex:row1];

    NSString *selecter2=[self.level2 objectAtIndex:row2];

    NSString *title=[[NSString alloc]initWithFormat:@"%@ | %@",selected1,selecter2];

    self.timeLabel.text=title;

    

}

-(void) initData{

    NSString *plistPath =  [[NSBundle mainBundle]pathForResource:@"province" ofType:@"plist"];

    NSDictionary *dictionary=[[NSDictionary alloc] initWithContentsOfFile:plistPath];

    self.allData=dictionary;

    

    self.level1=[self.allData allKeys];

    

    NSString *selectedLevel1=[self.level1 objectAtIndex:0];

    self.level2=[self.allData objectForKey:selectedLevel1];

    

}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return 2;

}

-(NSInteger)pickerView:(nonnull UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    if(component ==0){

        return [self.level1 count];

    }else{

        return [self.level2 count];

    }

}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    if(component == 0) {

        return [self.level1 objectAtIndex:row];

    } else {

        return [self.level2 objectAtIndex:row];

    }

}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    if(component == 0) {

        NSString *seletedP1 = [self.level1 objectAtIndex:row];

        NSArray *array = [self.allData objectForKey:seletedP1];

        self.level2 = array;

        [self.picjView reloadComponent:1];

    }

}

@end

运行结果:

https://www.jianshu.com/p/dd844288c5e3

发布了44 篇原创文章 · 获赞 9 · 访问量 8516

猜你喜欢

转载自blog.csdn.net/manmanlu2006/article/details/102370404