Objective-C|Objective-C 「传值」

Objective-C|Objective-C 「传值」
文章图片
Objective-C 一、属性传值

A界面->B界面传值
B_界面.h文件(接收者)
//////////B界面.h////////////1.声明属性 @property (nonatomic, copy) NSString *bString;

A_界面.m文件(发送者)
//////////A界面.m////////////2.属性传值 BViewController *bVC = [BViewController new]; bVC.bString = @"A"; [self.navigationController pushViewController:bVC animated:YES];

B_界面.m文件(接收者)
//////////B界面.m////////////3.结果 NSLog(@"A界面→B界面:%@", self.bString); //输出:A界面→B界面:A

二、方法传值
A界面->B界面传值
B_界面.m文件(接收者)
//////////B界面.m////////////1.自定义初始化方法 - (instancetype)initWithOne:(NSString *)one Two:(CGFloat)two{ self = [super init]; if (self) { //4.结果 NSLog(@"A界面→B界面:%@ %.2f", one, two); //输出: A界面→B界面:A 99.90 } return self; }

B_界面.h文件(接收者)
//////////B界面.h////////////2.声明初始化方法 - (instancetype)initWithOne:(NSString *)one Two:(CGFloat)two;

A_界面.m文件(发送者)
//////////A界面.m////////////3.方法传值 BViewController *bVC = [[BViewController alloc] initWithOne:@"A" Two:99.9]; [self.navigationController pushViewController:bVC animated:YES];

三、代理传值
B界面->A界面传值
B_界面.h文件(发送者)
//////////B界面.h//////////#import //1.声明协议和代理方法 @protocol BViewControllerDelegate - (void)protocolSendValue:(NSString *)value; @end@interface BViewController : UIViewController //2.设置代理(ARC用weak,MRC用assign) @property (nonatomic, weak) id myDelegate; @end

B_界面.m文件(发送者)
//////////B界面.m////////////3.让代理执行代理方法(代理传值) [self.myDelegate protocolSendValue:@"B"];

A_界面.m文件(接收者)
//////////A界面.m////////////4.签订代理协议 @interface AViewController () @end- (void)viewDidLoad { [super viewDidLoad]; //5.指定代理 BViewController *bVC = [BViewController new]; bVC.myDelegate = self; [self.navigationController pushViewController:bVC animated:YES]; }#pragma mark -BViewControllerDelegate //6.实现代理方法(结果) - (void)protocolSendValue:(NSString *)value{ NSLog(@"B界面→A界面:%@", value); //输出: B界面→A界面:B }

四、Block传值
B界面->A界面传值
注:Block中要防止在文件中循环引用
B_界面.h文件(发送者)
//////////B界面.h////////////1.声明Block属性 typedef void(^ValueBlock)(NSString *); @interface BViewController : UIViewController @property (nonatomic, copy) ValueBlock valueBlock; @end

B_界面.m文件(发送者)
//////////B界面.m////////////2.Block传值(代码块属性不为空) if (self.valueBlock) { self.valueBlock(@"B"); }

A_界面.m文件(接收者)
//////////A界面.m//////////BViewController *bVC = [BViewController new]; [self.navigationController pushViewController:bVC animated:YES]; //3.结果(Block中要防止在文件中循环引用) //__weak typeof(self) weakSelf = self; bVC.valueBlock = ^(NSString *string){ NSLog(@"B界面→A界面:%@", string); //输出: B界面→A界面:B };

五、通知中心传值
任意界面->A界面
注:发送者和接收者name参数必须保持一致
A_界面.m文件(接收者)
//////////A界面.m////////////1.注册通知 - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptNotification:) name:@"noticeSendValue" object:nil]; CViewController *cVC = [CViewController new]; [self.navigationController pushViewController:cVC animated:YES]; }//2.销毁通知 - (void)viewDidDisappear:(BOOL)animated{ [super viewDidDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self name:@"noticeSendValue" object:nil]; }//4.结果 - (void)acceptNotification:(NSNotification*)notification{ NSLog(@"%@",notification.userInfo); //输出:{name = text; } }

C_界面.m(发送者)
//////////C界面.m////////////3.通知传值 [[NSNotificationCenter defaultCenter] postNotificationName:@"noticeSendValue" object:self userInfo:@{@"name": @"text"}];

六、单例传值
A界面->任意界面
使用单例模式,编写ValueManager单例类来存储数据
ValueManager.h文件(全局唯一)
//////////单例.h//////////@interface ValueManager : NSObject + (ValueManager *)shareValueManager; @property (nonatomic, copy) NSString *strValue; @end

ValueManager.m文件(全局唯一)
//////////单例.m//////////+ (ValueManager *)shareValueManager{ static ValueManager *shareValueManager = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ shareValueManager = [ValueManager new]; }); return shareValueManager; }

A_界面.m文件(发送者)
//////////A界面.m////////////2.单例传值(存值) ValueManager *valueManager = [ValueManager shareValueManager]; valueManager.strValue = https://www.it610.com/article/@"Singleton";

C_界面.m文件(接收者)
//////////C界面.m////////////3.结果(取值) ValueManager *valueManager = [ValueManager shareValueManager]; NSLog(@"%@", valueManager.strValue); //输出: Singleton

七、NSuserDefault传值
A界面->任意界面
A_界面.m文件(发送者)
//////////A界面.m////////////1.NSuserDefault传值(存值) [[NSUserDefaults standardUserDefaults] setObject:@"NSUserDefaults" forKey:@"name"]; [[NSUserDefaults standardUserDefaults] synchronize];

【Objective-C|Objective-C 「传值」】C_界面.m文件(接收者)
//////////C界面.m////////////2.取值 NSLog(@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"name"]); //输出:NSUserDefaults

    推荐阅读