单例传值
//单例的.h文件
@property(strong,nonatomic)NSString *name;
+(userInfo *)shareinstence;
//单例的.m文件
static userInfo *instence=nil;
//第一种写法
//+(userInfo *)shareinstence
//{
// //懒加载
// if (instence==nil)
// {
// instence=[[userInfo alloc] init];
// }
// return instence;
//}
//第二种写法
+(userInfo *)shareinstence
{
@synchronized(self)
{
if (instence==nil)
{
instence=[[userInfo alloc] init];
}
return instence;
}
//懒加载
}
//重写allocWithZone方法
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
if (instence==nil)
{
instence=[super allocWithZone:zone];
}
return instence;
}
//重写copyWithZone
-(id)copyWithZone:(NSZone *)zone
{
return self;
}
//然后新建两个类
//第一个类.h
#import <UIKit/UIKit.h>
#import "userInfo.h"
#import "secondViewController.h"
@interface ViewController : UIViewController
@property(strong,nonatomic)UITextField *textname;
@end
//第一个类.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.textname=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];
self.textname.borderStyle=1;
[self.view addSubview:self.textname];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
userInfo *info=[userInfo shareinstence];
NSLog(@"%@",info.name);
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
userInfo *userinfo=[userInfo shareinstence];
userinfo.name=self.textname.text;
[self presentViewController:[secondViewController new] animated:YES completion:^{
NSLog(@"下一页");
}];
}
第二个类也是一样的道理
通知传值
创建两个类,继承于UIviewcontroller
第一个类.h
#import <UIKit/UIKit.h>
#import "secondViewController.h"
@interface ViewController : UIViewController
@property(strong,nonatomic)UITextField *txt;
@end
第一个类.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.txt=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];
self.txt.borderStyle=1;
[self.view addSubview:self.txt];
}
-(void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myPostValue:) name:@"LAY" object:nil];
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"LAY" object:nil];
}
-(void)myPostValue:(NSNotification *)noti
{
self.txt.text=noti.object;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
secondViewController *second=[[secondViewController alloc] init];
[self presentViewController:second animated:YES completion:^{
NSLog(@"下一页");
}];
}
第二个类的.h
#import <UIKit/UIKit.h>
遵循UITextFieldDelegate的协议
@interface secondViewController : UIViewController<UITextFieldDelegate>
@property(strong,nonatomic)NSString *str;
@property(strong,nonatomic)UITextField *txtinfo;
@end
第二个类的.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor grayColor];
NSLog(@"%@",self.str);
self.txtinfo=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 50)];
self.txtinfo.borderStyle=1;
self.txtinfo.delegate=self;
self.txtinfo.text=self.str;
[self.view addSubview:self.txtinfo];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//通知
NSNotification *notification=[[NSNotification alloc] initWithName:@"LAY" object:textField.text userInfo:nil];
[[NSNotificationCenter defaultCenter]postNotification:notification];
//隐藏键盘
if ([textField isFirstResponder])
{
[textField resignFirstResponder];
}
//隐藏页面
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"返回");
}];
return YES;
}