新建一个InforCenter类------------单例文件
InforCenter.h文件
#import <Foundation/Foundation.h>
@interface InforCenter : NSObject
@property (nonatomic,strong)NSString * userName;
@property (nonatomic,strong)NSString * height;
@property (nonatomic,strong)NSString * width;
#pragma mark - 单例
+(InforCenter *)sharedInforCenter;
@end
InforCenter.m文件
#import "InforCenter.h"
static InforCenter * inforcenter = nil;
@implementation InforCenter
#pragma mark - 单例
//通过单例这个类创建出来的对象指向同一个内存空间
+(InforCenter *)sharedInforCenter
{
if (!inforcenter) {
inforcenter = [[InforCenter alloc]init];
}
return inforcenter;
}
@end
ViewController.m文件
#import "ViewController.h"
#import "InforCenter.h"
#import "ShowViewController.h"
@interface ViewController ()
{
UITextField * _nameTF;
UITextField * _widthTF;
UITextField * _heightTF;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(100, 500, 120, 30)];
button.backgroundColor = [UIColor blackColor];
[button setTitle:@"传值" forState:UIControlStateNormal];
[button addTarget:self action:@selector(pressedButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
_nameTF = [[UITextField alloc]initWithFrame:CGRectMake(70, 50, 120, 30)];
_widthTF = [[UITextField alloc] initWithFrame:CGRectMake(70, 100, 120, 30)];
_heightTF = [[UITextField alloc] initWithFrame:CGRectMake(70, 150, 120, 30)];
_nameTF.placeholder = @"名字";
_widthTF.placeholder = @"宽度";
_heightTF.placeholder = @"高度";
_nameTF.layer.borderColor = [[UIColor blackColor] CGColor];
_widthTF.layer.borderColor = [[UIColor blackColor] CGColor];
_heightTF.layer.borderColor = [[UIColor blackColor] CGColor];
_nameTF.layer.borderWidth = 1;
_widthTF.layer.borderWidth = 1;
_heightTF.layer.borderWidth = 1;
[self.view addSubview:_nameTF];
[self.view addSubview:_widthTF];
[self.view addSubview:_heightTF];
}
- (void)pressedButton
{
InforCenter * someInfor = [InforCenter sharedInforCenter];
someInfor.userName = _nameTF.text;
someInfor.width = _widthTF.text;
someInfor.height = _heightTF.text;
ShowViewController * showVC = [[ShowViewController alloc] init];
[self presentViewController:showVC animated:YES completion:nil];
}
#pragma mark - 键盘收起
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];//设置点击空白地方键盘收起
}
@end
ShowViewController.m文件
#import "ShowViewController.h"
#import "InforCenter.h"
@interface ShowViewController ()
@end
@implementation ShowViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view .backgroundColor = [UIColor purpleColor];
self.view.alpha = 0.9;
UILabel * nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 50, 120, 30)];
UILabel * widthLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 100, 120, 30)];
UILabel * heightLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 150, 120, 30)];
nameLabel.textColor = [UIColor blackColor];
widthLabel.textColor = [UIColor blackColor];
heightLabel.textColor = [UIColor blackColor];
InforCenter * infor = [InforCenter sharedInforCenter];
nameLabel.text = infor.userName;
widthLabel.text = infor.width;
heightLabel.text = infor.height;
[self.view addSubview:nameLabel];
[self.view addSubview:widthLabel];
[self.view addSubview:heightLabel];
UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(100, 500, 120, 30)];
button.backgroundColor = [UIColor cyanColor];
[button setTitle:@"返回" forState:UIControlStateNormal];
[button addTarget:self action:@selector(pressedButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)pressedButton
{
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
@end
在ShowViewController类和ViewController类中引用了InforCenter里的属性值,单例的属性值是一个唯一存在的,调用获得的值也就是同一个。即可以用来传值。