iOS 界面传值的六种传值方式

实际开发场景中,页面之间传值非常的普遍,今天就来谈一谈ios 页面传值的六种方式。

页面传值也是指ViewController 传值。

1,属性传值

    顾名思义,在要跳转的页面设置一个属性,由页面1 跳转把值带过去即可。 

    NextViewController* nextVc = [[NextViewController alloc]init];
    nextVc.str = @"这是属性传值";
    [ self presentViewController:nextVc animated:YES completion:nil];

 2,单例传值

  创建一个单例对象,把数据放在单例对象里,启动第二个页面的时候,保存。第二个页面获取就行。

单例对象DefaultInstance.h文件:

@interface DefaultInstance : NSObject
@property(nonatomic,strong)NSString* str;
+ (instancetype)sharedInstance;
@end

单例对象DefaultInstance.m文件

#import "DefaultInstance.h"

@implementation DefaultInstance
//通过类方法创建单例对象
+ (instancetype)sharedInstance{
    
    static DefaultInstance * sharedVC = nil;
    if(sharedVC == nil){
        sharedVC = [[DefaultInstance alloc]init];
    }
    return sharedVC;
}
@end

 启动下个页面:

    DefaultNextViewController* nextVc = [[DefaultNextViewController alloc]init];
    [DefaultInstance sharedInstance].str = @"这是单例传值";
    [ self.navigationController pushViewController:nextVc animated:YES];

3,NSUserDefaults 传值

和单例传值很像,只不过 单例是写到内存里,NSUserDefaults 是写到 沙盒文件里。

示例如下:

保存数据启动下一个页面

   [[NSUserDefaults standardUserDefaults]setObject:_uiLabel.text forKey:@"intent"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    [self.navigationController pushViewController:defaultNext animated:YES];

获取数据:

 _uiTextField.text = [[NSUserDefaults standardUserDefaults]stringForKey:@"intent"];

4,代理传值

委托方需要创建一个代理,让代理方实现其方法,达到传值的目的。

委托方:

//委托方创建一个协议
@protocol pathValue <NSObject>
//定义一个方法
- (void)passValue:(NSString*)str;

@end
@interface DelegateNextViewController : UIViewController
@property(nonatomic,strong)NSString* sr;

//定义一个持有协议的id 指针
@property(weak)id<pathValue>delegate;
@end

代理方:

@interface DelegateViewController ()<pathValue>

 实现其方法:

-(void)passValue:(NSString *)str{
    _uiLabel.text = str;
}

 启动 下一个页面,并且设置代理 

    DelegateNextViewController* delegateNextVc = [[DelegateNextViewController alloc]init];
    delegateNextVc.delegate = self;
    [self.navigationController pushViewController:delegateNextVc animated:YES];

反向传值:

    [self.delegate passValue:@"这是反向传值"];
    [self.navigationController popViewControllerAnimated:YES];

5,block 传值

定义block:

@property(copy)void (^block)(NSString*);

启动设置block:

   BlockNextViewController* blockNextVc = [[BlockNextViewController alloc]init];
    blockNextVc.block = ^(NSString* str){
        self->_uiLabel.text = str;
    };
    [self.navigationController pushViewController:blockNextVc animated:YES];

反向传值:

     self.block(self.uiTextField.text);
    [self.navigationController popViewControllerAnimated:YES];

6,通知传值

通过发送一个通知消息来传值

注册通知:

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notHandle:) name:@"notify" object:nil];

 实现方法:

- (void)notHandle:(NSNotification*) not{
    self.title = not.userInfo[@"notHandle"];
}

发送消息:

 [[NSNotificationCenter defaultCenter] postNotificationName:@"notify" object:nil userInfo:@{@"notHandle":self.uiTextField.text}];

demo:

ios 传值

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值