delegate反向传值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#import "RootViewController.h"
#import "SecondViewController.h"
@interface RootViewController ()<SecondViewControllerDelegate>

@end
@implementation RootViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self createButton];
}
-(void)createButton{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 100);
button.backgroundColor = [UIColor lightGrayColor];
[button setTitle:@"下一页" forState:UIControlStateNormal];
[button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)onClick{
SecondViewController *secondVc = [[SecondViewController alloc]init];
secondVc.delegate = self;
[self.navigationController pushViewController:secondVc animated:YES];
}

-(void)changeColor:(UIColor *)color{
[self.view setBackgroundColor:color];
}

@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self createButton];

[self.view setBackgroundColor:[UIColor lightGrayColor]];
}
-(void)createButton{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 100);
button.backgroundColor = [UIColor lightGrayColor];
[button setTitle:@"上一页" forState:UIControlStateNormal];
[button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)onClick{
if (self.delegate && [self.delegate respondsToSelector:@selector(changeColor:)]) {
[self.delegate changeColor:[UIColor yellowColor]];
}
[self.navigationController popViewControllerAnimated:YES];
}
@end