五种方法
1.属性传值,适合界面A到界面B的传值
2.单例, 多个界面传值
3.通知 , 界面A跳转到界面C,C再跳回界面A的传值
4.使用代理, 界面A跳转到界面C,C再跳回界面A的传值
5.block
1.属性传值
新建工程,添加SecondViewController类,
在.h文件中,定义属性
@interface SecondViewController : UIViewController
@property (nonatomic, copy) NSString *name;
@end
在.m文件中
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor grayColor]];
self.navigationItem.title = @"第二页";
UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
lb.text = self.name;
[self.view addSubview:lb];
}
在ViewController.m文件中(使用导航控制器)
- (void)viewDidLoad {
[super viewDidLoad];
//设置导航控制器的标题
self.view.backgroundColor = [UIColor cyanColor];
self.navigationItem.title = @"主页";
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 80, 20)];
[btn1 setTitle:@"属性传值" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(nextVc) forControlEvents:UIControlEventTouchUpInside];
[btn1 setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:btn1];
}
按钮事件中编写代码
- (void)nextVc {
SecondViewController *vc = [[SecondViewController alloc]init];
vc.name =@"admin";
[self.navigationController pushViewController:vc animated:YES];
}
2.单例传值
理解为定义一个全局静态变量进行传值。
创建一个单例类,继承NSObject,命名为Session
Session.h文件中定义一个属性用来存储值,定义一个方法
@interface Session : NSObject
@property (nonatomic, copy) NSString *name;
+(instancetype)sharedName;
@end
Session.m文件实现
static Session *sessionHandle = nil;
@implementation Session
+(instancetype)sharedName{
if (sessionHandle == nil) {
sessionHandle = [[Session alloc]init];
}
return sessionHandle;
}
在ViewController.m文件中
- (void)viewDidLoad {
[super viewDidLoad];
//设置导航控制器的标题
self.view.backgroundColor = [UIColor cyanColor];
self.navigationItem.title = @"主页";
UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(100, 150, 80, 20)];
[btn2 setTitle:@"单例传值" forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(nextVc2) forControlEvents:UIControlEventTouchUpInside];
[btn2 setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:btn2];
}
在按钮事件中
- (void)nextVc2 {
ThirdViewController *vc = [[ThirdViewController alloc]init];
Session *s = [Session sharedName];
s.name = @"管理员";
[self.navigationController pushViewController:vc animated:YES];
}
添加ThirdViewController类,在.m文件中实现
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor grayColor]];
self.navigationItem.title = @"单例传值";
UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 70, 20)];
Session *s = [Session sharedName];
lb.text = s.name;
[lb setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:lb];
}
3.使用通知传值
以上是界面A传值到界面B,现在来实现界面A跳到界面C,界面C用通知传值给界面A。
添加FourthViewController类
在ViewController.m文件中
- (void)viewDidLoad {
[super viewDidLoad];
//设置导航控制器的标题
self.view.backgroundColor = [UIColor cyanColor];
self.navigationItem.title = @"主页";
UIButton *btn3 = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 80, 20)];
[btn3 setTitle:@"通知传值" forState:UIControlStateNormal];
[btn3 addTarget:self action:@selector(nextVc3) forControlEvents:UIControlEventTouchUpInside];
[btn3 setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:btn3];
//注册通知
//可以理解为创建一个收音机,调到FM电台,等待接收广播信息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(listen:) name:@"FM" object:nil];
}
在监听事件
-(void)listen:(NSNotification *)sender{
NSLog(@"您正在收听的是:%@",sender.userInfo[@"music"]);
}
按钮跳转事件
- (void)nextVc3 {
FourthViewController *vc = [[FourthViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
}
FourthViewController.m文件中实现
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor grayColor]];
self.navigationItem.title = @"通知传值";
UIButton *play = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 70, 20)];
[play setBackgroundColor:[UIColor blueColor]];
[play addTarget:self action:@selector(sendMusic) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:play];
}
-(void)sendMusic{
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"精忠报国" forKey:@"music"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"FM" object:nil userInfo:dict];
[self.navigationController popToRootViewControllerAnimated:YES];
}
4.使用代理传值
新建FivethViewController类,
FivethViewController.h定义
//1定义代理协议
@protocol FivethViewControllerDelegate
@optional
-(void)passValue:(NSString *)str;
@end
@interface FivethViewController : UIViewController
//2定义代理属性
@property (nonatomic, weak)id delegate;
@end
FivethViewController.m实现
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor grayColor]];
self.navigationItem.title = @"代理传值";
UIButton *play = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 70, 20)];
[play setBackgroundColor:[UIColor blueColor]];
[play addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:play];
}
-(void)test{
if ([self.delegate respondsToSelector:@selector(passValue:)]) {
[self.delegate passValue:@"代理传值成功"];
}
[self.navigationController popToRootViewControllerAnimated:YES];
}
源控制器ViewController.h文件中遵守代理协议
@interface ViewController ()
源控制器ViewController.m文件中
- (void)viewDidLoad {
[super viewDidLoad];
//设置导航控制器的标题
self.view.backgroundColor = [UIColor cyanColor];
self.navigationItem.title = @"主页";
UIButton *btn4 = [[UIButton alloc]initWithFrame:CGRectMake(100, 250, 80, 20)];
[btn4 setTitle:@"代理传值" forState:UIControlStateNormal];
[btn4 addTarget:self action:@selector(nextVc4) forControlEvents:UIControlEventTouchUpInside];
[btn4 setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:btn4];
}
- (void)nextVc4 {
FivethViewController *vc = [[FivethViewController alloc]init];
vc.delegate = self;
[self.navigationController pushViewController:vc animated:YES];
}
#pragma mark 实现代理方法
-(void)passValue:(NSString *)str{
NSLog(@"%@",str);
}
5.block传值
新建SixthViewController类,
SixthViewController.h定义
typedef void (^blockFunType)(NSString *);
@interface SixthViewController : UIViewController
@property (nonatomic, copy) blockFunType blockFun;
SixthViewController.m文件实现
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor grayColor]];
self.navigationItem.title = @"block传值";
UIButton *play = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 70, 20)];
[play setBackgroundColor:[UIColor blueColor]];
[play addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:play];
}
- (void)back{
self.blockFun(@"block传值");
[self.navigationController popViewControllerAnimated:YES];
}
源控制器ViewController界面拖进一个label
源控制器ViewController.h文件中
@property (weak, nonatomic) IBOutlet UILabel *label;
源控制器ViewController.m文件中
- (void)viewDidLoad {
[super viewDidLoad];
//设置导航控制器的标题
self.view.backgroundColor = [UIColor cyanColor];
self.navigationItem.title = @"主页";
UIButton *btn5 = [[UIButton alloc]initWithFrame:CGRectMake(100, 300, 80, 20)];
[btn5 setTitle:@"block传值" forState:UIControlStateNormal];
[btn5 addTarget:self action:@selector(nextVc5) forControlEvents:UIControlEventTouchUpInside];
[btn5 setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:btn5];
}
- (void)nextVc5 {
SixthViewController *vc = [[SixthViewController alloc]init];
__block typeof (self)tempSelf = self;
vc.blockFun = ^(NSString *string){
tempSelf.label.text = string;
NSLog(@"%@",string);
};
[self.navigationController pushViewController:vc animated:YES];
}