KVO相当观察者模式就要有观察者和被观察着要观察的数据,所以问题就来了,ios集成好了KVO模式直接调用方法使用就行了。
[myViewCotroller addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
myViewCotroller是被观察着 self是观察着 text是观察的值
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
[myViewCotroller removeObserver:self forKeyPath:@"text"];
[myViewCotroller removeObserver:self forKeyPath:@"text"];
//删除观察着观察的东西释放对象不然会崩溃切记。
上代码:
//
// ViewController.m
// helloworld
//
// Created by palm_city on 16/1/16.
// Copyright © 2016年 com.panshi. All rights reserved.
//
#import "ViewController.h"
#import "MyViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UpdateAlter>
{
UITableView *myTableView;
NSArray *tableArray;
NSMutableArray *tableArrayDetail;
MyViewController *myViewCotroller;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 30,320,400)];
myTableView.delegate = self;
myTableView.dataSource = self;
[self initWithData];
[self.view addSubview:myTableView];
}
-(void)initWithData
{
tableArray = [[NSArray alloc]initWithObjects:@"性别",@"年龄",@"性格取向",@"婚姻",@"地址",nil];
tableArrayDetail = [[NSMutableArray alloc]initWithObjects:@"男",@"20",@"保守",@"否",@"河南省南阳市",nil];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *string = @"Cell";
UITableViewCell *cell = [myTableView dequeueReusableCellWithIdentifier:string];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:string];
}
cell.textLabel.text = tableArray[indexPath.row];
cell.detailTextLabel.text = tableArrayDetail[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row)
{
case 0:
NSLog(@"1111111111111");
myViewCotroller = [[MyViewController alloc]init];
[myViewCotroller addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
[self.navigationController pushViewController:myViewCotroller animated:YES];
break;
case 1:
NSLog(@"222222222222222");
break;
case 2:
NSLog(@"3333333333333");
break;
case 3:
NSLog(@"44444444444444");
break;
case 4:
NSLog(@"55555555555");
break;
default:
break;
}
}
-(void)update:(NSString *)text
{
NSLog(@"%@",text);
tableArrayDetail[0] = text;
[myTableView reloadData];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
NSLog(@"%@",[myViewCotroller valueForKey:@"text"]);
tableArrayDetail[0] = [myViewCotroller valueForKey:@"text"];
[myTableView reloadData];
[myViewCotroller removeObserver:self forKeyPath:@"text"];
}
-(void)dealloc
{
NSLog(@"!!!!!!!!!!!!!!!!!!!!!");
}
@end
//
// ViewController.h
// helloworld
//
// Created by palm_city on 16/1/16.
// Copyright © 2016年 com.panshi. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// MyViewController.h
// helloworld
//
// Created by palm_city on 16/1/16.
// Copyright © 2016年 com.panshi. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol UpdateAlter <NSObject>
-(void)update:(NSString *)text;
@end
@interface MyViewController : UIViewController
@property(nonatomic,weak) id<UpdateAlter>delegate;
@property (nonatomic,strong) NSString *text;
@end
//
// MyViewController.m
// helloworld
//
// Created by palm_city on 16/1/16.
// Copyright © 2016年 com.panshi. All rights reserved.
//
#import "MyViewController.h"
@interface MyViewController()
{
NSString *KvoName;
NSString *KvoPrice;
UITextField *textfile;
}
@end
@implementation MyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self initwithMyView];
}
-(void)initwithMyView
{
textfile = [[UITextField alloc]initWithFrame:CGRectMake(30,100,100,30)];
textfile.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:textfile];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(140, 100, 60, 30)];
[button setTitle:@"Click" forState:0];
[button setTitleColor:[UIColor redColor] forState:0];
[button addTarget:self action:@selector(saveText:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)saveText:(UIButton *)sender
{
self.text = textfile.text;
[self.navigationController popViewControllerAnimated:YES];
}
- (void)dealloc
{
NSLog(@"DEalloc!!!!!");
}
@end