iOS页面通过KVO传值
来源:互联网 发布:里约奥运会直播软件 编辑:程序博客网 时间:2024/05/22 13:50
KVO相当观察者模式就要有观察者和被观察着要观察的数据,所以问题就来了,ios集成好了KVO模式直接调用方法使用就行了。
[myViewCotrolleraddObserver:selfforKeyPath:@"text"options:NSKeyValueObservingOptionNewcontext:nil];
myViewCotroller是被观察着 self是观察着 text是观察的值
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
[myViewCotrollerremoveObserver:selfforKeyPath:@"text"];
[myViewCotrollerremoveObserver:selfforKeyPath:@"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
{
[superviewDidLoad];
myTableView = [[UITableViewalloc]initWithFrame:CGRectMake(0,30,320,400)];
myTableView.delegate =self;
myTableView.dataSource =self;
[selfinitWithData];
[self.viewaddSubview:myTableView];
}
-(void)initWithData
{
tableArray = [[NSArrayalloc]initWithObjects:@"性别",@"年龄",@"性格取向",@"婚姻",@"地址",nil];
tableArrayDetail = [[NSMutableArrayalloc]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 = [myTableViewdequeueReusableCellWithIdentifier:string];
if(cell == nil)
{
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier: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 = [[MyViewControlleralloc]init];
[myViewCotrolleraddObserver:selfforKeyPath:@"text"options:NSKeyValueObservingOptionNewcontext:nil];
[self.navigationControllerpushViewController:myViewCotrolleranimated: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(@"%@",[myViewCotrollervalueForKey:@"text"]);
tableArrayDetail[0] = [myViewCotrollervalueForKey:@"text"];
[myTableView reloadData];
[myViewCotrollerremoveObserver:selfforKeyPath:@"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
{
[superviewDidLoad];
self.view.backgroundColor = [UIColorwhiteColor];
[selfinitwithMyView];
}
-(void)initwithMyView
{
textfile = [[UITextFieldalloc]initWithFrame:CGRectMake(30,100,100,30)];
textfile.backgroundColor = [UIColorlightGrayColor];
[self.viewaddSubview:textfile];
UIButton *button = [[UIButtonalloc] initWithFrame:CGRectMake(140,100, 60, 30)];
[button setTitle:@"Click"forState:0];
[button setTitleColor:[UIColorredColor] forState:0];
[button addTarget:selfaction:@selector(saveText:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
}
-(void)saveText:(UIButton *)sender
{
self.text =textfile.text;
[self.navigationControllerpopViewControllerAnimated:YES];
}
- (void)dealloc
{
NSLog(@"DEalloc!!!!!");
}
@end
- iOS页面通过KVO传值
- ios kvo
- IOS KVO
- ios KVO
- iOS KVO
- IOS kvo
- iOS--KVO
- iOS --KVO
- ios kvo
- ios KVO
- iOS-KVO
- iOS --KVO
- iOS --- KVO
- iOS KVO
- ios-KVO
- iOS KVO 通过change值获取 CGPoint 的坐标点
- IOS页面之间通过委托(协议)传值
- ios学习--kvo/kvc
- Openstack-nova
- mysql远程防火墙3306端口
- 模拟实现智能指针
- Json解析值get与opt的区别
- android中使用wifi来adb
- iOS页面通过KVO传值
- react native Command /bin/sh failed with exit code 1( at process._tickCallback (node.js:336:13))
- 通过web连接页面打开app在 appstore中的详情
- 深谈读研如何提高技术
- 学习历程《C语言陷阱和缺陷》
- 使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境
- 一款好用的备忘录制作实现
- CM和CMTS是怎么上网的
- 008-LAMP_MySQL_DML