用非代理的方式在A、B两个页面之间实现值的传递
思路如下:
在A页面声明一个公有属性Label,然后在B页面使用presentingViewController方法去获取弹窗消失后的视图,将B页面上的TextField的内容传给Label。
完整代码:
A页面
.h文件
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@property (nonatomic, strong) UILabel *label;
@end
.m文件
#import "RootViewController.h"
#import "ModalViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1) 设置背景颜色
self.view.backgroundColor = [UIColor cyanColor];
//2) 设置一个Label
//a) 创建一个Label
_label = [[UILabel alloc] initWithFrame:CGRectMake(25, 100, 270, 30)];
//b) 设置该label的tag
_label.tag = 1000;
//c) 设置label的内容
_label.text = @"非代理传值";
//d) 设置背景颜色
_label.backgroundColor = [UIColor orangeColor];
//e) 设置字体颜色
_label.textColor = [UIColor whiteColor];
//f) 设置居中方式
_label.textAlignment = NSTextAlignmentCenter;
//g) 添加label
[self.view addSubview:_label];
//3) 设置跳转你的button
//a) 创建button
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//b) 设置其frame
button.frame = CGRectMake(0, 0, 200, 30);
//c) 设置其在屏幕的中心
button.center = self.view.center;
//d) 设置背景颜色
button.backgroundColor = [UIColor lightGrayColor];
//e) 设置显示的内容
[button setTitle:@"跳转" forState:UIControlStateNormal];
//f) 设置相应事件
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//g) 添加到页面上
[self.view addSubview:button];}
#pragma mark - 点击的按键响应
- (void) buttonAction: (UIButton *) button {
//1. 初始化
ModalViewController *modalViewController = [[ModalViewController alloc] init];
//2. 弹窗模式
modalViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
//3. 模态视图
[self presentViewController:modalViewController animated:YES completion:nil];
}
@end
B页面
.h文件
#import <UIKit/UIKit.h>
@interface ModalViewController : UIViewController
@end
.m文件
#import "ModalViewController.h"
#import "RootViewController.h"
@interface ModalViewController ()
@end
@implementation ModalViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1) 设置背景颜色
self.view.backgroundColor = [UIColor orangeColor];
//2) 设置一个TextField
//a) 创建一个TextField
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(25, 100, 270, 30)];
//b) 设置tag textField.tag = 2000;
//c) 设置显示的效果
textField.borderStyle = UITextBorderStyleRoundedRect;
//d) 显示提示语
textField.placeholder = @"请输入一段文字...";
//e) 添加到self.view上
[self.view addSubview:textField];
//3) 设置跳转你的button
//a) 创建button
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//b) 设置其frame
button.frame = CGRectMake(0, 0, 200, 30);
//c) 设置其在屏幕的中心
button.center = self.view.center;
//d) 设置背景颜色
button.backgroundColor = [UIColor lightGrayColor];
//e) 设置显示的内容
[button setTitle:@"返 回" forState:UIControlStateNormal];
//f) 设置相应事件
[button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
//g) 添加到页面上
[self.view addSubview:button];}
- (void) backAction: (UIButton *) button {
//1) 取值
UITextField *textField = (UITextField *)[self.view viewWithTag:2000];
//2) 修改值
//说明:presentingViewController这个方法是获取到谁弹出了该视图控制器,也就是获取到弹窗消失后的视图
RootViewController *presentingVC = (RootViewController *)self.presentingViewController;
presentingVC.label.text = textField.text;
//3) 关闭模态视图
[self dismissViewControllerAnimated:YES completion:nil];
}
@end