代理就是自己的事让别人来做
在OC中就是协议方要做的事给代理方来实现
背景情况
A父界面 B子界面 当B返回A的时候 要让A的背景颜色变为红色 这就要给A传一个color的参数
写两个ViewController
在RootViewController.h中写
#import <UIKit/UIKit.h>
#import "WJJFirstViewController.h"
<span style="color:#FF0000;">//4、把协议进入进来
@interface WJJRootViewController : UIViewController <WJJFirstViewControllerDelegate>
</span>
@end
在RootViewController.m中写入
#import "WJJRootViewController.h"
@interface WJJRootViewController ()
@end
@implementation WJJRootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self createRightBarButtonItem];
}
- (void)createRightBarButtonItem{
UIButton * rightBarButton = [UIButton buttonWithType:UIButtonTypeSystem];
[rightBarButton setTitle:@"nextPage" forState:UIControlStateNormal];
rightBarButton.frame = CGRectMake(0, 0, 50, 20);
rightBarButton.titleLabel.font = [UIFont systemFontOfSize:10];
rightBarButton.tintColor = [UIColor redColor];
[rightBarButton addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButton];
}
- (void)nextPage{
WJJFirstViewController * first = [[WJJFirstViewController alloc] init];
<span style="color:#FF0000;">//5、让协议方的代理 设置为自己
first.delegate = self;</span>
[self.navigationController pushViewController:first animated:YES];
}
#pragma mark ---delegate
<span style="color:#FF0000;">//6、实现代理方法
- (void)changeRootViewBackgroudColorWithColor:(UIColor *)color{
self.view.backgroundColor = color;
}
</span>
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
然后再FirstViewController.h中写入下列代码
#import <UIKit/UIKit.h>
<span style="color:#FF0000;">//1、创建代理
@protocol WJJFirstViewControllerDelegate <NSObject>
- (void)changeRootViewBackgroudColorWithColor:(UIColor *)color;
@end
</span>
@interface WJJFirstViewController : UIViewController
<span style="color:#FF0000;">//2、用该代理写一个属性
@property (nonatomic,strong) id <WJJFirstViewControllerDelegate> delegate;
</span>
@end
然后再FirstViewController.m中写入
#import "WJJFirstViewController.h"
@interface WJJFirstViewController ()
@end
@implementation WJJFirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor orangeColor];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
<span style="color:#FF0000;"> //3、当页面消失时 让它的代理实行代理方法 且把参数传进去
[self.delegate changeRootViewBackgroudColorWithColor:[UIColor redColor]];</span>
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end