IOS传值之代理传值(一)

时间:2023-03-09 16:02:53
IOS传值之代理传值(一)

1、使用代理delegate的方法

2、使用通知Notification的方法

3、KVO等方法

4、block传值

~~~~~~~~~~~~~~~~

1、使用代理delegate的方法

#import "ViewController.h"

#import "SubViewController.h"

@interface ViewController ()<subViewDelegate>

@property(nonatomic,strong)UILabel *label;

@end

@implementation ViewController

-(UILabel *)label{

if (_label==nil) {

_label=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 150, 30)];

_label.backgroundColor=[UIColor grayColor];

//[self.view addSubview:_label];

}

return _label;

}

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

[self.view addSubview:self.label];

UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 30)];

[button setBackgroundColor:[UIColor grayColor]];

[button setTitle:@"next" forState:UIControlStateNormal];

[button addTarget:self action:@selector(nextVC) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button];

}

-(void)nextVC{

NSLog(@"hhhhhhhhh");

SubViewController *subVC=[[SubViewController alloc]init];

subVC.delegate=self;

//[self.navigationController pushViewController:subVC animated:YES];

[self presentViewController:subVC animated:YES completion:^{

NSLog(@"cccccc");

}];

}

-(void)showStringFromSubView:(NSString *)textFieldtext{

self.label.text=textFieldtext;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

~~~~~~~~~~~~~~~~~~~~~~~

#import <UIKit/UIKit.h>

@protocol subViewDelegate <NSObject>

-(void)showStringFromSubView:(NSString *)textFieldtext;

@end

@interface SubViewController : UIViewController

@property(nonatomic,weak)id<subViewDelegate> delegate;

@end

#import "SubViewController.h"

@interface SubViewController ()

@property(nonatomic,strong)UITextField *textField;

@end

@implementation SubViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor=[UIColor whiteColor];

[self.view addSubview:self.textField];

//backBtn

UIButton *back= [UIButton buttonWithType:UIButtonTypeCustom];

back.frame=CGRectMake(100, 200, 50, 50);

[back setBackgroundColor:[UIColor grayColor]];

[back setTitle:@"back" forState:UIControlStateNormal];

[back addTarget:self action:@selector(backToPre) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:back];

}

-(void)backToPre{

[self.delegate showStringFromSubView:self.textField.text];

[self dismissViewControllerAnimated:YES completion:^{

NSLog(@"backToPre");

}];

}

-(UITextField *)textField{

if (_textField==nil) {

_textField=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 30)];

_textField.backgroundColor=[UIColor grayColor];

}

return _textField;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~实现效果:

IOS传值之代理传值(一)IOS传值之代理传值(一)

IOS传值之代理传值(一)IOS传值之代理传值(一)