iOS页面通过KVO传值

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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值