您的位置:首页 > 移动开发 > IOS开发

iOS反向传值-----系统单例方法

2015-10-30 00:00 411 查看
摘要: 反向传值有: 1.代理传值
下级界面要把textField的内容 传给 上一级,这时下级界面就可以委托上级界面 修改 label的值
第二个界面(主动方) 可以制定一个协议 规范代理的行为,

第一个界面(被动方) 遵守协议 作为 代理

2.单例传值 1.系统单例 2.自定义单例
UIApplication 单例传值
第二个界面 把值给单例
第一个从单例中获取值
3.通知传值
4.NSUserDefaults
5.block传值

新建一个空工程 在appdelegate中的.h填写如下代码

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

//保存字符串
@property (nonatomic,copy) NSString *labelStr;

@end

在RootViewController如下代码:

#import "RootViewController.h"
#import "MyControl.h"
#import "SecondViewController.h"
#import "AppDelegate.h"

#define kDebugPrint NSLog(@"%s",__func__)
@interface RootViewController ()
{
UILabel *_label;
}
@end

@implementation RootViewController

/*
正向传值
创建第一个界面   通过第一个界面跳转到第二个界面
如果由第一个界面向第二个界面 进行传值 正向传值
属性传值

第二张向第一张界面传值 反向传值

下级界面向上一级界面传值---》反向传值

反向传值方式:

1.代理传值
下级界面要把textField的内容 传给 上一级,这时下级界面就可以委托上级界面 修改 label的值
第二个界面(主动方) 可以制定一个协议 规范代理的行为,

第一个界面(被动方) 遵守协议 作为 代理

2.单例传值 1.系统单例 2.自定义单例
UIApplication 单例传值
第二个界面 把值给单例
第一个从单例中获取值

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];
}
//界面将要显示的时候
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//将要显示的时候 从单例中获取 值

UIApplication *app = [UIApplication sharedApplication];
AppDelegate *appDelegate = app.delegate;

//修改值
_label.text = appDelegate.labelStr;

}

- (void)btnClick:(UIButton *)btn {
//每次点击按钮  都会创建一个新的第二张对象
SecondViewController *svc = [[SecondViewController alloc] init];

[self presentViewController:svc animated:YES completion:nil];
[svc release];

}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

在SecondViewController如下代码

#import "SecondViewController.h"
#import "MyControl.h"
#define kDebugPrint NSLog(@"%s",__func__)
#import "AppDelegate.h"

@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 {
//点击传值按钮先把 数据给 单例
//UIApplication
//获取单例
UIApplication *app = [UIApplication sharedApplication];
//得到AppDelegate
AppDelegate *appDelegate = app.delegate;

//传值 把值 赋给appDelegate的属性
appDelegate.labelStr = _textField.text;

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息