实现效果:
搭建两个页面, 两个页面中分别添加一个Label和一个TextField控件, 实现前后单例传值操作
Label1———>TextField2
Label2———>TextField1
*重点部分,单例类的建立
<<DataHandle.h文件>>
#import <Foundation/Foundation.h>
@interface DataHandle : NSObject
//给单例添加一个属性, 用来传值
@property (strong, nonatomic) NSString *text;
//定义一个初始化单例的方法
+ (instancetype)sharedDataHandel;
@end
<<DataHandle.m文件>>
//写单例的原因, 是希望在程序的运行期间, 他在内存中一直存在, 可以随时读取数据
static DataHandle *dataHandle = nil;
@implementation DataHandle
//创建单例方法使用加号的原因: 在创建之前无法存在一个实例对象去去调用
+ (instancetype)sharedDataHandel{
//为了线程安全(添加线程锁)
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (!dataHandle) {
dataHandle = [[DataHandle alloc] init];
}
});
return dataHandle;
}
//第二种方法
//+(instancetype)sharedDataHandle{
//为了线程安全(添加线程锁)
// @synchronized(self) {
// if (!dataHandle) {
// dataHandle = [[DataHandle alloc] init];
// }
// }
// return dataHandle;
//}
@end
*新建两个viewController , 实现两个ViewController页面的跳转传值
<<FirstViewController.m文件>>
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "DataHandle.h"
@interface FirstViewController ()
@property (strong, nonatomic) UILabel *label;
@property (strong, nonatomic) UITextField *textField;
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed:0.595 green:0.955 blue:1.000 alpha:1.000];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"next" style:(UIBarButtonItemStylePlain) target:self action:@selector(nextAC)];
self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
self.label.backgroundColor = [UIColor orangeColor];
[self.view addSubview:self.label];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 150, 200, 60)];
self.textField.backgroundColor = [UIColor orangeColor];
[self.view addSubview:self.textField];
}
//将从后往前的传值, 放到该方法中是因为ViewDidLoad方法只加载一遍
- (void)viewWillAppear:(BOOL)animated{
self.label.text = [DataHandle sharedDataHandel].text;
}
- (void)nextAC{
SecondViewController *secondVC = [SecondViewController new];
//传值
DataHandle *dataHandle = [DataHandle sharedDataHandel];
dataHandle.text = self.textField.text;
[self.navigationController pushViewController:secondVC animated:YES];
}
}
@end
<<SecondViewController.m文件>>
#import "SecondViewController.h"
#import "DataHandle.h"
@interface SecondViewController ()
@property (strong, nonatomic) UILabel *label;
@property (strong, nonatomic) UITextField *textField;
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:(UIBarButtonItemStylePlain) target:self action:@selector(backAC)];
self.view.backgroundColor = [UIColor colorWithRed:1.000 green:0.399 blue:0.584 alpha:1.000];
self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
self.label.backgroundColor = [UIColor colorWithRed:0.680 green:1.000 blue:0.618 alpha:1.000];
[self.view addSubview:self.label];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 150, 200, 60)];
self.textField.backgroundColor = [UIColor colorWithRed:0.800 green:1.000 blue:0.653 alpha:1.000];
[self.view addSubview:self.textField];
//取值
DataHandle *dataHandle = [DataHandle sharedDataHandel];
self.label.text = dataHandle.text;
}
- (void)backAC{
//传值
DataHandle *dataHandle = [DataHandle sharedDataHandel];
dataHandle.text = self.textField.text;
[self.navigationController popViewControllerAnimated:YES];
}
@end```