单例传值
- 单例可以保证其某个类的实例在程序中是唯一的,便于进行资源和数据的共享
- 单例传值可以理解为定义一个全局静态变量进行传值,将第二个页面的内容传入第一个页面并显示。
- 单例可以正向、逆向传值.但是如果想要正向传值的话,可以直接简单粗暴地使用属性传值….下面我们来谈谈单例的逆向传值…
3.新建一个Person类
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic,strong) NSString *string;
+(instancetype)shareInstance;
@end
#import "Person.h"
@implementation Person
//声明一个static变量
static Person *instrance = nil;
//使用类方法生成这个类唯一的实例
+(Person *)shareInstance{
if (instrance == nil) {
instrance = [[super alloc]init];
}
return instrance;
}
@end
4.然后在SecondViewController.m中导入头文件,并且声明一个UITextField 和 UIbutton的属性.创建一个UITextField 和 UIbutton
#import "SecondViewController.h"
#import "Person.h"
@interface SecondViewController ()
@property (nonatomic,strong) UITextField *textF;
@property (nonatomic,strong) UIButton *backBtn;
@end
@implementation SecondViewController
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
_textF.text = [Person shareInstance].string;
}
-(UITextField *)textF{
if (!_textF) {
_textF = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 50)];
_textF.borderStyle = UITextBorderStyleRoundedRect;
_textF.backgroundColor = [UIColor whiteColor];
}
return _textF;
}
-(UIButton *)backBtn{
if (!_backBtn ) {
_backBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_backBtn.frame = CGRectMake(20, 200, self.view.bounds.size.width-40, 44);
_backBtn.backgroundColor = [UIColor blueColor];
_backBtn.tintColor = [UIColor whiteColor];
_backBtn.titleLabel.font = [UIFont systemFontOfSize:25.0];
[_backBtn setTitle:@"Back" forState:UIControlStateNormal];
[_backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _backBtn;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.textF];
[self.view addSubview:self.backBtn];
}
-(void)backAction:(UIButton *)sender{
NSLog(@"%p",[Person shareInstance]);
// 返回时为单例属性赋值
[Person shareInstance].string = _textF.text;
[self.navigationController popViewControllerAnimated:YES];
5.回到ViewController.m中
#import "ViewController.h"
#import "Person.h"
#import "SecondViewController.h"
@interface ViewController ()
@property (nonatomic,strong)UITextField *textField;
@property (nonatomic,strong)UIButton *btn ;
@end
@implementation ViewController
-(UITextField *)textField{
if (!_textField) {
_textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 50)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.backgroundColor = [UIColor whiteColor];
}
return _textField;
}
-(UIButton *)btn{
if (!_btn) {
_btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_btn.frame = CGRectMake(20, 200, self.view.bounds.size.width- 40, 40);
_btn.backgroundColor = [UIColor blueColor];
_btn.titleLabel.font = [UIFont systemFontOfSize:25.0];
[_btn setTitle:@"Next" forState:UIControlStateNormal];
[_btn setTintColor:[UIColor whiteColor]];
[_btn addTarget:self action:@selector(NextAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _btn;
}
- (void)viewDidLoad {
[super viewDidLoad];
/**
* 单例传值
单例可以保证其某个类的实例在程序中是唯一的,便于进行资源和数据的共享
单例传值可以理解为定义一个全局静态变量进行传值,将第二个页面的内容传入第一个页面并显示。
*/
self.navigationItem.title = @"单例传值";
self.view. backgroundColor = [UIColor yellowColor];
[self.view addSubview:self.textField];
[self.view addSubview:self.btn];
}
//接收由第二个界面pop回时textF里面的数据
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
_textField.text = [Person shareInstance].string;
}
-(void)NextAction:(UIButton *)sender{
// 跳转到下个界面的同时为单例属性赋值
_textField.text = [Person shareInstance].string;
SecondViewController *secondVC = [[SecondViewController alloc]init];
[self.navigationController pushViewController:secondVC animated:YES];
}
6.由于我们是通过navigationController push到第二个页面的,所以,我们要在AppDelegate创建一个UINavigationController
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];
[self.window makeKeyAndVisible];
return YES;
}
7.模拟器运行效果如下