控制器之间的数据传递——全局变量传值

时间:2022-02-27 20:41:50

全局变量传值

一. 实现步骤

  1. 在界面2中声明一个全局变量,并且用它保存需要传递的值

  2. 在界面1中用extern声明界面2中的全局变量,就可使用他所保存的值赋值

二. extern优缺点

  1. 优点

    a.全局可视,任何一个函数都可以访问和更改变量值
    b.内存地址固定,读写效率高

  2. 缺点

    a.容易造成命名冲突
    b.当值不正确或者出错时,很难确定是哪个函数更改过这个变量
    c.不支持多线程

三. 具体实现

1. AppDelegate类

---------- AppDelegate.m文件

#import "AppDelegate.h"
#include "OneViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// 1. 创建窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

// 2. 创建窗口的根控制器
// 2.1 创建导航控制器的根控制器
UIViewController *oneVc = [[OneViewController alloc] init];
// 2.2 创建导航控制器
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:oneVc];
// 2.3 给窗口设置根控制器
self.window.rootViewController = nav;

// 3. 设置窗口为主窗口并显示窗口
[self.window makeKeyAndVisible];

// 隐藏导航控制器的导航条
nav.navigationBarHidden = YES;

return YES;
}

@end

2. OneViewController类

---------- OneViewController.m文件

#import "OneViewController.h"
#import "TwoViewController.h"

// 必须要用extern关键字修饰,才能使用
extern NSString *PassData;

@interface OneViewController ()

@property (nonatomic,strong) UITextField *textField;

@end

@implementation OneViewController

- (void)viewDidLoad {
[super viewDidLoad];

//设置控制器View的背景颜色
self.view.backgroundColor = [UIColor greenColor];

// 创建点击按钮
UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
clickBtn.frame = CGRectMake(10, 100, 80, 40);
clickBtn.backgroundColor = [UIColor whiteColor];
[clickBtn setTitle:@"到界面2" forState:UIControlStateNormal];
[clickBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[clickBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[clickBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:clickBtn];

// 创建文本框
_textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 150, 250, 40)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_textField];

}

#pragma mark - 点击事件

- (void)btnClick {

// 创建界面2,并压入栈
TwoViewController *twoVc = [[TwoViewController alloc] init];
[self.navigationController pushViewController:twoVc animated:YES];

}

- (void)viewWillAppear:(BOOL)animated {

_textField.text = PassData;

}

@end

3. TwoViewController类

---------- TwoViewController.m文件

#import "TwoViewController.h"

// 设置全局变量,用于在不同的文件中传值
NSString *PassData;

@interface TwoViewController ()

@property (nonatomic,strong) UITextField *textField;

@end

@implementation TwoViewController

- (void)viewDidLoad {
[super viewDidLoad];

//设置控制器View的背景颜色
self.view.backgroundColor = [UIColor yellowColor];

// 创建点击按钮
UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
clickBtn.frame = CGRectMake(10, 100, 80, 40);
clickBtn.backgroundColor = [UIColor whiteColor];
[clickBtn setTitle:@"传 值" forState:UIControlStateNormal];
[clickBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[clickBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[clickBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:clickBtn];

// 创建文本框
_textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 150, 250, 40)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_textField];
}

#pragma mark - 点击事件

- (void)btnClick {

PassData = _textField.text;

// 跳转界面
[self.navigationController popViewControllerAnimated:YES];

}

@end