IOS开发之利用单例模式完成两个ViewController之间的传值
单例文件内容如下
.h
@interface Single:NSObject @property(nonatomic, copy)NSURL *value; +(instancetype)sharedInstance; @end
.m
@implementation Single +(instancetype)sharedInstance{ static Single *myInstance = nil; if(myInstance == nil){ myInstance = [[Single alloc]init]; } return myInstance; } @end
A向Bcontroller传值
A里导入单例类
[Single sharedInstance].value=toUrl;
B中
NSURL *url=[Single sharedInstance].value;
这是最简单的一种页面间传值方法
测试打印结果如下
2020-06-19 11:20:58.596871+0800 RecordDem[1443:469673]页面二准备复制给单例对象的URL是file:///var/mobile/Containers/Data/Application/6887E45D-0C41-4CD0-8D7A-5829C3B36571/Documents/2020-06-18-20:16:03.caf
2020-06-19 11:20:54.763083+0800 RecordDem[1443:469673] 页面三file:///var/mobile/Containers/Data/Application/6887E45D-0C41-4CD0-8D7A-5829C3B36571/Documents/2020-06-18-20:16:03.caf
页面3顺利拿到了页面2传的URL值