界面跳转传值
顺向传值
- 在目标的.h文件中,定义一个属性
// 定义一个属性,正向传值
@property (strong, nonatomic) NSString* passedValue;
- 直接用代码,在目标的.h文件中,定义数据变量
// 界面传值
RedViewController* vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"aaa"];
vc.passedValue = @"11111";
[self presentViewController:vc animated:YES completion:nil];
- 使用id时候
// 重写这个方法,获取目标ViewController,完成真正的传值
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
RedViewController* vc = segue.destinationViewController;
vc.passedValue = @"1aaaaa";
}
逆向传值,代理和block有点像回调
- 使用代理方式(delegate)
-
- 申明代理
@protocol passValueProtocol <NSObject>
-
- // 定义属性
@property (assign, nonatomic) id<passValueProtocol> delegate;
-
- 传值
[self.delegate passValue:self.content.text];
- 使用block来传值
-
- // 定义一个block
typedef void (^ PassValueBlock)(NSString* info);
-
- // 申明属性
@property (copy, nonatomic) PassValueBlock passValueBlock;
-
- // 传值
if (self.passValueBlock) {
self.passValueBlock(self.content.text);
}
- 通知传值
-
- 注册通知
// 注册监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlerNoti:) name:@"getValue" object:nil];
-
- 发送通知(很少使用)
//监听传值
[[NSNotificationCenter defaultCenter]postNotificationName:@"getValue" object:nil userInfo:@{@"info": self.content.text}];
代码展示
B.h文件
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
// 定义一个block
typedef void (^ PassValueBlock)(NSString* info);
// 定义协议
@protocol passValueProtocol <NSObject>
// 定义一个代理方法
-(void) passValue: (NSString*) value;
@end
@interface RedViewController : UIViewController
// 正向传值
// 定义一个属性,正向传值
@property (strong, nonatomic) NSString* passedValue;
// 逆向传值
// 定义代理属性
@property (assign, nonatomic) id<passValueProtocol> delegate;
// 定义block属性
@property (copy, nonatomic) PassValueBlock passValueBlock;
@end
NS_ASSUME_NONNULL_END
.m文件
#import "RedViewController.h"
@interface RedViewController ()
// 一个lable用户显示正向传值
@property (weak, nonatomic) IBOutlet UILabel *lable;
// 一个输入框用于逆向传值
@property (weak, nonatomic) IBOutlet UITextField *content;
@end
@implementation RedViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.lable.text = self.passedValue;
// Do any additional setup after loading the view.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self dismissViewControllerAnimated:YES completion:nil];
}
// 点击按钮逆向传值
- (IBAction)clickValue:(id)sender {
// 代理传值
[self.delegate passValue:self.content.text];
// 使用block
if (self.passValueBlock) {
self.passValueBlock(self.content.text);
}
//监听传值
[[NSNotificationCenter defaultCenter]postNotificationName:@"getValue" object:nil userInfo:@{@"info": self.content.text}];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
A.m
- (void)viewDidLoad {
[super viewDidLoad];
// 注册监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlerNoti:) name:@"getValue" object:nil];
}
-(void) handlerNoti: (NSNotification*) not {
NSDictionary* userInfo = not.userInfo;
NSLog(@"获取通知传值%@", userInfo[@"info"]);
}
- (IBAction)clickToUIViewController:(id)sender {
// 界面传值
RedViewController* vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"aaa"];
// 正向传值
vc.passedValue = @"11111";
// 代理逆向传值
vc.delegate = self;
// block逆向传值
vc.passValueBlock = ^(NSString * _Nonnull info) {
NSLog(@"block获取值为%@", info);
};
[self presentViewController:vc animated:YES completion:nil];
}
// 重写这个方法,获取目标ViewController,实现真正的传值
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
RedViewController* vc = segue.destinationViewController;
// 正向传值,通过segua连线
vc.passedValue = @"1aaaaa";
}
// segua跳转
- (IBAction)clickSegua:(id)sender {
[self performSegueWithIdentifier:@"abcd" sender:nil];
}