KVC-KVO:利用KVC-KVO观察者模式实现页面的传值。

K(key)键值  V(value)值   C(change)变化  O(observe)观察
这是OC中的语法:作用当某个对象的值发生变化时,通知某个函数,在函数内部,能够获取到变化的值。
框架中:通知中心。
使用场合:在音乐播放的过程中,如果选择的歌曲发生变化,立刻通知播放器,播放新选择的歌曲。

 1 #import "RootViewController.h"
 2 #import "DetailViewController.h"
 3 @interface RootViewController ()
 4 {
 5     UITableView *tv;
 6     DetailViewController *detail;
 7 }
 8 @property NSString  *sendValue;  //存放点击某一行的值
 9 @end
10 
11 @implementation RootViewController
12 
13 - (void)viewDidLoad {
14     [super viewDidLoad];
15     // Do any additional setup after loading the view.
16     
17     detail=[[DetailViewController alloc] init];
18     //添加观察者
19     [self addObserver:detail forKeyPath:@"sendValue" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];
20     
21     [self createUI ];
22 }
23 -(void)createUI{
24     tv=[[UITableView alloc] initWithFrame:CGRectMake(0, 20, 375, 600) style:UITableViewStylePlain];
25     tv.delegate=self;
26     tv.dataSource=self;
27 //    tv.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
28     [self.view addSubview:tv];
29 }
30 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
31     return 30;
32 }
33 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
34     NSString *identifier=@"kvckvo";
35     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
36     if (!cell) {
37         cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
38     }
39     cell.textLabel.text=[NSString stringWithFormat:@"%ld",indexPath.row+1];
40     return cell;
41 }
42 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
43 
44     [self presentViewController:detail animated:NO completion:nil];
45     //赋值操作
46     self.sendValue=[NSString stringWithFormat:@"%ld",indexPath.row+1];
47     
48 }
49 - (void)didReceiveMemoryWarning {
50     [super didReceiveMemoryWarning];
51     // Dispose of any resources that can be recreated.
52 }
 1 #import "DetailViewController.h"
 2 @interface DetailViewController ()
 3 
 4 @end
 5 
 6 @implementation DetailViewController
 7 @synthesize lblData;
 8 @synthesize btnReturn;
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11     // Do any additional setup after loading the view.
12     lblData=[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
13     [self.view addSubview:lblData];
14     btnReturn=[[UIButton alloc] initWithFrame:CGRectMake(100, 150, 100, 30)];
15     btnReturn.backgroundColor=[UIColor redColor];
16     [btnReturn setTitle:@"返回" forState:UIControlStateNormal];
17     [btnReturn addTarget:self action:@selector(btn_Return:) forControlEvents:UIControlEventTouchUpInside];
18     [self.view addSubview:btnReturn];
19 }
20 -(void)btn_Return:(UIButton *)sender{
21     [self dismissViewControllerAnimated:NO completion:nil];
22 }
23 //重写通知中心函数
24 -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
25     NSString *strValue=change[@"new"];
26     lblData.text=strValue;
27 }
28 - (void)didReceiveMemoryWarning {
29     [super didReceiveMemoryWarning];
30     // Dispose of any resources that can be recreated.
31 }

 

posted @ 2015-09-14 19:06  crushing  阅读(294)  评论(0编辑  收藏  举报