ios界面传值方式

界面跳转传值

顺向传值
  1. 在目标的.h文件中,定义一个属性
	// 定义一个属性,正向传值
	@property (strong, nonatomic) NSString* passedValue;
  1. 直接用代码,在目标的.h文件中,定义数据变量
    // 界面传值
    RedViewController* vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"aaa"];
    vc.passedValue = @"11111";
    [self presentViewController:vc animated:YES completion:nil];
  1. 使用id时候
// 重写这个方法,获取目标ViewController,完成真正的传值
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    RedViewController* vc = segue.destinationViewController;
    vc.passedValue = @"1aaaaa";
}

逆向传值,代理和block有点像回调

  1. 使用代理方式(delegate)
    1. 申明代理
@protocol passValueProtocol <NSObject>

    1. // 定义属性
@property (assign, nonatomic) id<passValueProtocol> delegate;

    1. 传值
[self.delegate passValue:self.content.text];

  1. 使用block来传值
    1. // 定义一个block
typedef void (^ PassValueBlock)(NSString* info);

    1. // 申明属性
@property (copy, nonatomic) PassValueBlock passValueBlock;

    1. // 传值
    if (self.passValueBlock) {
        self.passValueBlock(self.content.text);
    }
  1. 通知传值
    1. 注册通知
    // 注册监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlerNoti:) name:@"getValue" object:nil];
    1. 发送通知(很少使用)
    //监听传值
    [[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];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值