很少写协议,一般是在cell的按钮部分写一个协议,这部分主要讲通知,这是自己写的一个小例子,是实现了的
1.VCOne
#import "HomeViewController.h"
#import "OneViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"首页";
self.view.backgroundColor = [UIColor whiteColor];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:btn];
[btn setTitle:@"跳转" forState:0];
[btn setTitleColor:[UIColor blackColor] forState:0];
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor = [UIColor purpleColor];
btn.backgroundColor = [UIColor purpleColor];
//通知注册了一下
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:@"test" object:nil];
}
//如果后边发送通知,会调用这个方法
- (void)test
{
NSLog(@"=================");
}
-(void)click:(UIButton *)btn{
self.view.backgroundColor = [UIColor whiteColor];
[self.navigationController pushViewController:OneViewController.new animated:YES];
}
2.VCTwo
#import "OneViewController.h"
#import "TwoViewController.h"
@interface OneViewController ()
@end
@implementation OneViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:btn];
[btn setTitle:@"跳转" forState:0];
[btn setTitleColor:[UIColor blackColor] forState:0];
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor = [UIColor purpleColor];
//通知注册一下
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:@"test" object:nil];
}
//如果后边发送通知调用这个方法
- (void)test
{
NSLog(@"=================");
}
-(void)click:(UIButton *)btn{
self.view.backgroundColor = [UIColor whiteColor];
[self.navigationController pushViewController:TwoViewController.new animated:YES];
}
3.VCThree
#import "TwoViewController.h"
@interface TwoViewController ()
@end
@implementation TwoViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor greenColor];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:btn];
[btn setTitle:@"通知" forState:0];
[btn setTitleColor:[UIColor blackColor] forState:0];
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor = [UIColor purpleColor];
}
-(void)click:(UIButton *)btn{
//发送通知,前边两个注册过的都可以收到
[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil];
}