iOS的五种传值

前言

iOS常见的五种传值分别为属性传值,通知传值,代理传值,block传值,单例传值

属性传值

用于正向传值,简单,但对象之间得相关联
模拟一个场景, A->B(A传值B)
B.h文件定义一个变量


@interface lastViewController : UIViewController
@property(nonatomic,strong)NSString * str;
@end

A.m文件跳转并传值

    lastViewController * last = [[lastViewController alloc]init];
    // [[NSNotificationCenter defaultCenter]postNotificationName:nsnotificationName object:nil];
    last.str = self.textField.text;
    [self.navigationController pushViewController:last animated:YES];
}

通知传值

不需要编写多少代码,实现比较简单,对象可以不关联,跨层传值,一对多(发送一个通知,订阅该通知的都会得到相应,是同步的),通知使用过多显得太乱,不好调试
模拟一个场景 : C->B && C->A
订阅通知,尽量在delloc里移除该通知,注册通知和移除通知是成双成对(iOS8之前),这里附上两个链接
iOS尽量不要在viewWillDisappear:方法中移除通知
iOS 9 NSNotificationCenter移除问题
A订阅通知

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(value:) name:nsnotificationName object:nil];
}

A销毁移除通知

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

或者移除单个通知

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:nsnotificationName object:nil];
}

B和A控制器一样,最后在C发送通知

NSDictionary * dict = @{@"key":self.textField.text};
[[NSNotificationCenter defaultCenter]postNotificationName:nsnotificationName object:nil userInfo:dict];

记得先订阅,在发送再能接收到通知的

代理传值

代理传值一对一,可以定义多个方法,但定义代码太过繁琐
逆向传值 B->A
B.h : 声明协议,定义代理执行的方法,并定义一个该协议的属性

@protocol lastViewControllerDelegate<NSObject>
//方法可实现的
@optional
//方法必须实现
//@required
-(void)pushViewcontroler:(NSString *)str;
@end
@interface lastViewController : UIViewController
@property(nonatomic,weak)id<lastViewControllerDelegate>delagate;
@end

B.m

//设置视图将要消失时.通过代理传值
//判断代理是否存在,并且设置代理能够响应代理方法时,才执行代理方法
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (self.delagate && [self.delagate respondsToSelector:@selector(pushViewcontroler:)]) {
        [self.delagate pushViewcontroler:@"哈哈"];
    }
}

A.m 服从协议,设置代理,并实现协议的方法

//服从协议
@interface ViewController ()<lastViewControllerDelegate>
@end

- (IBAction)push:(id)sender {
    lastViewController * last = [[lastViewController alloc]init];
    //设置代理
    last.delagate = self;
    [self.navigationController pushViewController:last animated:YES];
}
//实现协议方法
-(void)pushViewcontroler:(NSString *)str{
    self.textField.text = str;
}

block传值

代码简洁,反向传值,模拟场景 B->A
B.h声明block属性

@property(nonatomic,strong)void(^Myblcok)(NSString * str);

B.m调用该block

self.Myblcok([self.showLabel.text substringToIndex:<#(NSUInteger)#>]);

A.m 接收该block

 lastViewController * last = [[lastViewController alloc]init];
    last.Myblcok = ^(NSString *str) {
        self.textField.text = str;
    };
    last.str = self.textField.text;
    [self.navigationController pushViewController:last animated:YES];

block简单定义使用可以看看这篇 : iOS之轻松上手block(上)

单例传值

  1. 创建一个单例
@interface model : NSObject
@property(nonatomic,strong)NSString * str;
//暴露一个方法,供外界访问
+(model *)del;
@end
static  model * del = nil;
@implementation model
+(instancetype)allocWithZone:(struct _NSZone *)zone{
    return [model del];
}
+(model *)del{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        del = [[super allocWithZone:nil]init];
    });
    return del;
}
//在通过拷贝的时候创建对象时,会调用到-(id)copyWithZone:(NSZone *)zone,-(id)mutableCopyWithZone:(NSZone *)zone方法。因此,可以重写这些方法,让创建的对象唯一。
+ (id)copyWithZone:(struct _NSZone *)zone{
    return [model del];
}
+ (id)mutableCopyWithZone:(struct _NSZone *)zone{
    return [model del];
}
@end

2.A中为单例的属性赋值

- (void)viewDidLoad {
    [super viewDidLoad];
    model * del = [model del];
    del.str = @"我是单例的属性";
}

3.B中获取单例属性

- (void)viewDidLoad {
    [super viewDidLoad];
    model * del = [model del];
    self.showLabel.text = del.str;
}

了解单例的创建可以看看 :iOS中的单例模式

感言

这次是我对iOS最开始学习的内容回顾,自己的理解,有不对的望指出,谢谢

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS开发中,页面传值是很常见的,但是页面传值你究竟知道多少呢?笔者这篇文章就是给大家介绍一下页面传值的具体方式,...
    蒲公英少年带我飞阅读 6,516评论 10 45
  • 设计模式 1.delegate和notification什么区别,什么情况使用? 2.描述一下KVO和KVC。 K...
    丶逐渐阅读 5,952评论 3 2
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,387评论 30 472
  • 内容丰富的2017,导致写新年祝词的时候例外的晚点。内容一多,实在是不知道该从哪开始讲起了。 变化大概是这一年的主...
    低徊人阅读 955评论 0 0
  • 有人给我留言: 我是英语专业,大学毕业就回家乡了,在一个镇上当初中英语老师,可是每天都很不开心。很想辞职,可是感觉...
    杨小米阅读 37,181评论 15 125