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





0 0
原创粉丝点击