界面传值的方式

iOS传值的方式

  1. 属性传值(正向传值)
  2. 方法传值(正向传值)
  3. 代理传值
  4. block属性传值(反向传值)
  5. block方法传值(反向传值)
  6. KVO(反向传值)
  7. KVC(正向传值)
  8. 通知notification
  9. 单例模式方式
  10. UserDefault或者文件方式

注:没有备注传值方向的可以用于正向传值/反向传值

代码示例图:

代码详情:

VCPassByValue.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

//定义代理协议
@protocol VCPassByValueDelegate <NSObject>

//定义一个协议函数
-(void)sendTextContent:(NSString*)content;

@end

@interface VCPassByValue : UIViewController

//定义一个代理对象 来实现协议函数,达到代理对象改变本身属性的目的
//代理对象一定要实现代理协议
@property(nonatomic,strong)id<VCPassByValueDelegate>pbv1delegate;

@property(nonatomic,strong)UILabel* mlabel02;

@property(nonatomic,strong)UILabel* mlabel03;

@property(nonatomic,copy)NSString* mName;

+(instancetype)initInstance;//共享单例方法

@end

NS_ASSUME_NONNULL_END

VCPassByValue.m

#import "VCPassByValue.h"
#import "VCPassByValue2.h"

@interface VCPassByValue ()<VCPassByValue2Delegate>
{
    VCPassByValue2* _pbv2;
    UITextField* _textField01;
    UITextField* _textField02;
    UITextField* _textField03;
    UITextField* _textField04;
    UITextField* _textField05;
    UITextField* _textField06;
    UILabel* _label01;
    UILabel* _label02;
    UILabel* _label03;
    UILabel* _label04;
}

@end

//创建单例
static VCPassByValue* singlePbv = nil;

@implementation VCPassByValue

@synthesize mlabel02 = _label02;
@synthesize mlabel03 = _label03;

//方式一:性能低
//+(instancetype)initInstance{
//    @synchronized (self) {//任意对象
//        if (instance == nil) {
//            instance = [[VCPassByValue alloc]init];
//        }
//    }
//    return instance;
//}

//方式二:性能高
+ (instancetype)initInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[VCPassByValue alloc]init];
    });
    return instance;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initTitle];
    [self initView];
}

-(void)initTitle{
    UIImage *backImage = [UIImage imageNamed:@"back"];
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]initWithImage:backImage style:UIBarButtonItemStylePlain target:self action:@selector(pressBack)];
    UIBarButtonItem* rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"下一页" style:UIBarButtonItemStyleDone target:self action:@selector(pressBtn)];
    
    self.navigationItem.leftBarButtonItem = leftBtn;
    self.navigationItem.rightBarButtonItem = rightBtn;
    self.title = @"第一个界面";
    self.tabBarController.tabBar.hidden = YES;
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

-(void)pressBack{
    [self.navigationController popViewControllerAnimated:NO];
}

-(void)initView{

    UIButton* btn01 = [[UIButton alloc]initWithFrame:CGRectMake(260, 30, 100, 40)];
    [btn01 setTitle:@"发送" forState:UIControlStateNormal];
    [btn01 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn01 setBackgroundColor:[UIColor brownColor]];
    [btn01 addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn01];
    
    _textField01 = [[UITextField alloc]initWithFrame:CGRectMake(50, 30, 180, 40)];
    [_textField01 setPlaceholder:@"属性传值"];
    [_textField01 setBorderStyle:UITextBorderStyleRoundedRect];
    [self.view addSubview:_textField01];
    
    UIButton* btn02 = [[UIButton alloc]initWithFrame:CGRectMake(260, 90, 100, 40)];
    [btn02 setTitle:@"发送" forState:UIControlStateNormal];
    [btn02 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn02 setBackgroundColor:[UIColor brownColor]];
    [btn02 addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn02];
    
    _textField02 = [[UITextField alloc]initWithFrame:CGRectMake(50, 90, 180, 40)];
    [_textField02 setPlaceholder:@"方法传值"];
    [_textField02 setBorderStyle:UITextBorderStyleRoundedRect];
    [self.view addSubview:_textField02];
    
    UIButton* btn03 = [[UIButton alloc]initWithFrame:CGRectMake(260, 150, 100, 40)];
    [btn03 setTitle:@"发送" forState:UIControlStateNormal];
    [btn03 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn03 setBackgroundColor:[UIColor brownColor]];
    [btn03 addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn03];
    
    _textField03 = [[UITextField alloc]initWithFrame:CGRectMake(50, 150, 180, 40)];
    [_textField03 setPlaceholder:@"代理正向传值"];
    [_textField03 setBorderStyle:UITextBorderStyleRoundedRect];
    [self.view addSubview:_textField03];
    
    _label01 = [[UILabel alloc]initWithFrame:CGRectMake(50, 200, 350, 40)];
    [_label01 setText:@"代理反向传值:"];
    [self.view addSubview:_label01];
    
    _label02 = [[UILabel alloc]initWithFrame:CGRectMake(50, 250, 350, 40)];
    [_label02 setText:@"block属性反向传值:"];
    [self.view addSubview:_label02];
    
    _label03 = [[UILabel alloc]initWithFrame:CGRectMake(50, 300, 350, 40)];
    [_label03 setText:@"block方法反向传值:"];
    [self.view addSubview:_label03];
    
    _label04 = [[UILabel alloc]initWithFrame:CGRectMake(50, 350, 350, 40)];
    [_label04 setText:@"KVO反向传值:"];
    [self.view addSubview:_label04];
    
    UIButton* btn04 = [[UIButton alloc]initWithFrame:CGRectMake(260, 410, 100, 40)];
    [btn04 setTitle:@"发送" forState:UIControlStateNormal];
    [btn04 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn04 setBackgroundColor:[UIColor brownColor]];
    [btn04 addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn04];

    _textField04 = [[UITextField alloc]initWithFrame:CGRectMake(50, 410, 180, 40)];
    [_textField04 setPlaceholder:@"通知传值"];
    [_textField04 setBorderStyle:UITextBorderStyleRoundedRect];
    [self.view addSubview:_textField04];
    
    UIButton* btn05 = [[UIButton alloc]initWithFrame:CGRectMake(260, 470, 100, 40)];
    [btn05 setTitle:@"发送" forState:UIControlStateNormal];
    [btn05 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn05 setBackgroundColor:[UIColor brownColor]];
    [btn05 addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn05];
    
    _textField05 = [[UITextField alloc]initWithFrame:CGRectMake(50, 470, 180, 40)];
    [_textField05 setPlaceholder:@"单例传值"];
    [_textField05 setBorderStyle:UITextBorderStyleRoundedRect];
    [self.view addSubview:_textField05];
    

    UIButton* btn06 = [[UIButton alloc]initWithFrame:CGRectMake(260, 530, 100, 40)];
    [btn06 setTitle:@"发送" forState:UIControlStateNormal];
    [btn06 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn06 setBackgroundColor:[UIColor brownColor]];
    [btn06 addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn06];
    
    _textField06 = [[UITextField alloc]initWithFrame:CGRectMake(50, 530, 180, 40)];
    [_textField06 setPlaceholder:@"KVC传值"];
    [_textField06 setBorderStyle:UITextBorderStyleRoundedRect];
    [self.view addSubview:_textField06];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [_textField01 resignFirstResponder];
    [_textField02 resignFirstResponder];
    [_textField03 resignFirstResponder];
    [_textField04 resignFirstResponder];
    [_textField05 resignFirstResponder];
    [_textField06 resignFirstResponder];
}

//代理反向传值
- (void)sendMessage:(NSString *)message{
    NSString* str01 = [NSString stringWithFormat:@"代理反向传值:%@",message];
    [_label01 setText:str01];
}

-(void)pressBtn{
    [VCPassByValue initInstance].mName = _textField05.text;//单例传值
    
    _pbv2 = [[VCPassByValue2 alloc]initWithNibName:_textField02.text bundle:nil];
    _pbv2.propertyValue = _textField01.text;//属性传值
    [_pbv2 setValue:_textField06.text forKey:@"mKvcText"];//kvc传值
    self.pbv1delegate = (id)_pbv2;//正向代理传值
    _pbv2.pbv2delegate = self;//反向代理传值
    
    //代理对象调用事件函数
    [_pbv1delegate sendTextContent:_textField03.text];
    
    //block属性反向传值
    _pbv2.receiveValue = ^(NSString * _Nonnull value) {
        __weak VCPassByValue *pbv = self;
        NSString* str02 = [NSString stringWithFormat:@"block属性反向传值:%@",value];
        [pbv.mlabel02 setText:str02];
    };

    //注册观察者  KVO传值  forKeyPath键值必须与下一界面的属性值名称相同
    [_pbv2 addObserver:self forKeyPath:@"data" options:NSKeyValueObservingOptionNew context:nil];

    //通知传值
    [[NSNotificationCenter defaultCenter]postNotificationName:@"send" object:_textField04.text userInfo:@{@"content":_textField04.text}];
    
    _textField01.text = @"";
    _textField02.text = @"";
    _textField03.text = @"";
    _textField04.text = @"";
    _textField05.text = @"";
    _textField06.text = @"";
    [_textField01 resignFirstResponder];
    [_textField02 resignFirstResponder];
    [_textField03 resignFirstResponder];
    [_textField04 resignFirstResponder];
    [_textField05 resignFirstResponder];
    [_textField06 resignFirstResponder];
    
    [self.navigationController pushViewController:_pbv2 animated:NO];

}

//KVO回调
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"data"]) {
        NSString* str04 = [NSString stringWithFormat:@"KVO反向传值:%@",_pbv2.data];
        [_label04 setText:str04];
    }
}

//移除KVO
- (void)dealloc
{
    [_pbv2 removeObserver:self forKeyPath:@"data"];
}

//block方法反向传值
- (void)viewDidAppear:(BOOL)animated{
    [_pbv2 sendBlockMessage:@"第一页" andBlock:^(NSString * _Nonnull str) {
        NSString* str02 = [NSString stringWithFormat:@"block方法反向传值:%@",str];
        self.mlabel03.text = str02;
    }];
}

@end

VCPassByValue2.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

typedef void(^ReceiveValueBlock)(NSString* value);

typedef void(^VCPassByValueBlock)(NSString* str);

@protocol VCPassByValue2Delegate <NSObject>

-(void)sendMessage:(NSString*)message;

@end

@interface VCPassByValue2 : UIViewController

@property(nonatomic,strong)NSString* propertyValue;

@property(nonatomic,strong)NSString* methodValue;

@property(nonatomic,strong)id<VCPassByValue2Delegate>pbv2delegate;

@property(nonatomic,copy)ReceiveValueBlock receiveValue;

-(void)sendBlockMessage:(NSString*)msg andBlock:(VCPassByValueBlock)block;

@property(nonatomic,strong)UILabel* mlabel04;

@property(nonatomic,strong)NSString* data;

@property(nonatomic,strong)NSString* mKvcText;

@end

NS_ASSUME_NONNULL_END

VCPassByValue2.m

#import "VCPassByValue2.h"
#import "VCPassByValue.h"

@interface VCPassByValue2 ()<VCPassByValueDelegate>
{
    UITextField* _textField01;
    UITextField* _textField02;
    UITextField* _textField03;
    UITextField* _textField04;
    UITextField* _textField05;
}
@end

@implementation VCPassByValue2

@synthesize mlabel04 = _label04;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initTitle];
    [self initView];
}

//代理正向传值
- (void)sendTextContent:(NSString *)content{
    UILabel* _label03 = [[UILabel alloc]initWithFrame:CGRectMake(50, 130, 350, 40)];
    NSString* str03 = [NSString stringWithFormat:@"代理正向传值:%@", content];
    [_label03 setText:str03];
    [self.view addSubview:_label03];
}

-(void)initTitle{
    UIImage *backImage = [UIImage imageNamed:@"back"];
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]initWithImage:backImage style:UIBarButtonItemStylePlain target:self action:@selector(pressBack)];

    self.navigationItem.leftBarButtonItem = leftBtn;
    self.title = @"第二个界面";
    self.tabBarController.tabBar.hidden = YES;
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

-(void)pressBack{
    [self.navigationController popViewControllerAnimated:NO];
}

-(void)initView{
    UILabel* _label01 = [[UILabel alloc]initWithFrame:CGRectMake(50, 30, 350, 40)];
    NSString* str01 = [NSString stringWithFormat:@"属性值:%@",_propertyValue];
    [_label01 setText:str01];
    [self.view addSubview:_label01];

    UILabel* _label02 = [[UILabel alloc]initWithFrame:CGRectMake(50, 80, 350, 40)];
    NSString* str02 = [NSString stringWithFormat:@"方法值:%@",_methodValue];
    [_label02 setText:str02];
    [self.view addSubview:_label02];

    UIButton* btn01 = [[UIButton alloc]initWithFrame:CGRectMake(260, 190, 100, 40)];
    [btn01 setTitle:@"发送" forState:UIControlStateNormal];
    [btn01 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn01 setBackgroundColor:[UIColor brownColor]];
    [btn01 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn01];
    
    _textField01 = [[UITextField alloc]initWithFrame:CGRectMake(50, 190, 180, 40)];
    [_textField01 setPlaceholder:@"代理反向传值"];
    [_textField01 setBorderStyle:UITextBorderStyleRoundedRect];
    [self.view addSubview:_textField01];

    UIButton* btn02 = [[UIButton alloc]initWithFrame:CGRectMake(260, 250, 100, 40)];
    [btn02 setTitle:@"发送" forState:UIControlStateNormal];
    [btn02 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn02 setBackgroundColor:[UIColor brownColor]];
    [btn02 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn02];
    
    _textField02 = [[UITextField alloc]initWithFrame:CGRectMake(50, 250, 180, 40)];
    [_textField02 setPlaceholder:@"block属性反向传值"];
    [_textField02 setBorderStyle:UITextBorderStyleRoundedRect];
    [self.view addSubview:_textField02];
    
    UIButton* btn03 = [[UIButton alloc]initWithFrame:CGRectMake(260, 310, 100, 40)];
    [btn03 setTitle:@"发送" forState:UIControlStateNormal];
    [btn03 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn03 setBackgroundColor:[UIColor brownColor]];
    [btn03 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn03];
    
    _textField03 = [[UITextField alloc]initWithFrame:CGRectMake(50, 310, 180, 40)];
    [_textField03 setPlaceholder:@"block方法反向传值"];
    [_textField03 setBorderStyle:UITextBorderStyleRoundedRect];
    [self.view addSubview:_textField03];
    
    UIButton* btn04 = [[UIButton alloc]initWithFrame:CGRectMake(260, 370, 100, 40)];
    [btn04 setTitle:@"发送" forState:UIControlStateNormal];
    [btn04 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn04 setBackgroundColor:[UIColor brownColor]];
    [btn04 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn04];
    
    _textField04 = [[UITextField alloc]initWithFrame:CGRectMake(50, 370, 180, 40)];
    [_textField04 setPlaceholder:@"KVO反向传值"];
    [_textField04 setBorderStyle:UITextBorderStyleRoundedRect];
    [self.view addSubview:_textField04];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNoti:) name:@"send" object:nil];
    
    _label04 = [[UILabel alloc]initWithFrame:CGRectMake(50, 420, 350, 40)];
    [self.view addSubview:_label04];
    
    UILabel* _label05 = [[UILabel alloc]initWithFrame:CGRectMake(50, 470, 350, 40)];
    NSString* str05 = [NSString stringWithFormat:@"单例传值:%@",[VCPassByValue initInstance].mName];
    [_label05 setText:str05];
    [self.view addSubview:_label05];
    
    UILabel* _label06 = [[UILabel alloc]initWithFrame:CGRectMake(50, 520, 350, 40)];
    NSString* str06 = [NSString stringWithFormat:@"KVC传值:%@",self.mKvcText];
    [_label06 setText:str06];
    
    [self.view addSubview:_label06];
}

//方法传值
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    _methodValue = nibNameOrNil;
    return self;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
 
    [_textField01 resignFirstResponder];
    [_textField02 resignFirstResponder];
    [_textField03 resignFirstResponder];
    [_textField04 resignFirstResponder];

}

//block方法反向传值
-(void)sendBlockMessage:(NSString *)msg andBlock:(VCPassByValueBlock)block{
    block(_textField03.text);
    _textField03.text = @"";
}

-(void)pressBtn:(UIButton*)btn{
 
    [_pbv2delegate sendMessage:_textField01.text];//代理反向传值
    self.receiveValue(_textField02.text);//block属性反向传值
    self.data = _textField04.text;//KVO传值
    
    _textField01.text = @"";
    _textField02.text = @"";
    _textField04.text = @"";
    [_textField01 resignFirstResponder];
    [_textField02 resignFirstResponder];
    [_textField03 resignFirstResponder];
    [_textField04 resignFirstResponder];
    
    [self.navigationController popViewControllerAnimated:NO];
    
}

-(void)receiveNoti:(NSNotification *)noti{
    NSLog(@"object=====%@",noti.object);
    NSLog(@"userInfo=====%@",noti.userInfo);
    NSString* str04 = [NSString stringWithFormat:@"通知传值:%@",noti.object];
    [_label04 setText:str04];
}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值