新建一个appdelegate
接着新建一个RootViewController SecondViewController
#import "RootViewController.h"
#import "MyControl.h"
#import "SecondViewController.h"
#define kDebugPrint NSLog(@"%s",__func__)
//遵守协议
@interface RootViewController ()<ChangeTextDelegate>//遵守协议
{
UILabel *_label;
}
@end
@implementation RootViewController
/*
正向传值
创建第一个界面 通过第一个界面跳转到第二个界面
如果由第一个界面向第二个界面 进行传值 正向传值
属性传值
第二张向第一张界面传值 反向传值
下级界面向上一级界面传值---》反向传值
反向传值方式:
1.代理传值
第二个界面要把textField的内容 传给 第一个界面,
第一个界面 可以修改 label的值
这时第二个界面就可以 委托 第一个界面 修改 label的值(第二个界面要向第一个界面传数据)
第二个界面(主动方) 可以制定一个协议 规范代理的行为,
第一个界面(被动方) 遵守协议 作为 代理
2.单例传值 1.系统单例 2.自定义单例
3.通知传值
4.NSUserDefaults
5.block传值
*/
- (void)viewDidLoad {
[super viewDidLoad];
[self showUI];
}
- (void)showUI {
self.view.backgroundColor = [UIColor grayColor];
_label = [MyControl creatLabelWithFrame:CGRectMake(0, 30, 300, 30) text:@"XXX"];
_label.backgroundColor = [UIColor yellowColor];
[self.view addSubview:_label];
UIButton *button = [MyControl creatButtonWithFrame:CGRectMake(10, 200, 300, 50) target:self sel:@selector(btnClick:) tag:201 image:nil title:@"切换到第二张"];
[self.view addSubview:button];
}
#pragma mark - 代理实现协议方法
- (void)changeLabelTextWithString:(NSString *)string {
_label.text = string;
}
- (void)btnClick:(UIButton *)btn {
//每次点击按钮 都会创建一个新的第二张对象
SecondViewController *svc = [[SecondViewController alloc] init];
svc.delegate = self;//设置代理
[self presentViewController:svc animated:YES completion:nil];
[svc release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
//主动方制定协议
//规范代理的行为
@protocol ChangeTextDelegate <NSObject>
- (void)changeLabelTextWithString:(NSString *)string;
@end
//==========================上面是协议的格式形式
@interface SecondViewController : UIViewController
//设置代理 弱引用
@property (nonatomic,assign) id <ChangeTextDelegate>delegate;
@end
#import "SecondViewController.h"
#import "MyControl.h"
#define kDebugPrint NSLog(@"%s",__func__)
@interface SecondViewController ()
{
UITextField *_textField;
}
@end
@implementation SecondViewController
- (void)dealloc {
kDebugPrint;
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
[self showUI];
}
- (void)showUI {
UIButton *button = [MyControl creatButtonWithFrame:CGRectMake(10, 30, 300, 30) target:self sel:@selector(btnClick:) tag:301 image:nil title:@"返回"];
[self.view addSubview:button];
UIButton *button2 = [MyControl creatButtonWithFrame:CGRectMake(10,200 , 300, 30) target:self sel:@selector(btnClick2:) tag:302 image:nil title:@"传值"];
[self.view addSubview:button2];
_textField = [MyControl creatTextFieldWithFrame:CGRectMake(10, 100, 300, 30) placeHolder:nil delegate:nil tag:100];
[self.view addSubview:_textField];
}
//收键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[_textField resignFirstResponder];
}
- (void)btnClick:(UIButton *)btn {
//返回上一级
[self dismissViewControllerAnimated:YES completion:nil];
}
//传值按钮
- (void)btnClick2:(UIButton *)btn {
//传值 把值传给第一个界面
if ([self.delegate respondsToSelector:@selector(changeLabelTextWithString:)]) {
//通知代理 调用方法 并且把_textField.text传给代理
//代理修改label的值
[self.delegate changeLabelTextWithString:_textField.text];
}else {
NSLog(@"代理没有实现方法");
}
}
@end