總結(jié) iOS 日常開發(fā)中的幾種常用傳值方式:
- 正向傳值
- 代理傳值
- block傳值
- 通知傳值
- 單例
文章代碼:https://github.com/BWLi420/BWTransferValueDemo
1. 正向傳值
- 使用場(chǎng)景:界面 A 跳轉(zhuǎn)界面 B 的同時(shí)褂痰,向界面 B 傳遞值憔鬼,即上級(jí)向下級(jí)傳遞
- 步驟:
-
在界面 B 的 .h 文件中公開聲明一個(gè)屬性
@interface ViewC1 : UIViewController //正向傳值 1 @property (nonatomic, copy) NSString *value_1; @end
-
導(dǎo)入界面 B 的頭文件犹撒,并使用 B 創(chuàng)建出一個(gè)對(duì)象,對(duì)屬性進(jìn)行賦值
ViewC1 *VC1 = [[ViewC1 alloc] init]; VC1.value_1 = @"我是正向傳值"; [self.navigationController pushViewController:VC1 animated:YES];
-
2. 代理傳值
- 使用場(chǎng)景:界面 A 跳轉(zhuǎn)界面 B ,界面 B 返回界面 A 時(shí)傳結(jié)果給 A,即下級(jí)向上級(jí)傳遞
- 步驟:
-
在界面 B 頭文件中定義協(xié)議
@protocol ViewC2Delegate <NSObject> - (void)changeMainVCBackgroundColor:(UIColor *)color; @end
-
在界面 B 頭文件中定義代理屬性
@interface ViewC2 : UIViewController @property (nonatomic, weak) id<ViewC2Delegate> delegate; @end
-
在界面 B 的 .m 中合適位置調(diào)用代理方法
// 3.調(diào)用代理方法 <需先進(jìn)行檢測(cè)> if ([self.delegate respondsToSelector:@selector(changeMainVCBackgroundColor:)]) { [self.delegate changeMainVCBackgroundColor:[UIColor greenColor]]; }
在界面 A 中設(shè)置代理
-
在界面 A 中遵守協(xié)議
@interface ViewController ()<ViewC2Delegate> @end ViewC2 *VC2 = [[ViewC2 alloc] init]; VC2.delegate = self; [self.navigationController pushViewController:VC2 animated:YES];
-
實(shí)現(xiàn)代理協(xié)議中的方法
- (void)changeMainVCBackgroundColor:(UIColor *)color { self.view.backgroundColor = color; }
-
3. block傳值
- 使用場(chǎng)景:類似代理,當(dāng)協(xié)議中的方法只有一個(gè)倒源,可以使用 block,此時(shí)可以不用寫協(xié)議纪铺,相當(dāng)于簡化的代理
- 步驟:
-
在發(fā)送者頭文件中定義 block 屬性
@interface ViewC3 : UIViewController @property (nonatomic, copy) void (^changeMainVCBgColor)(UIColor *color); @end
-
在發(fā)送者 .m 文件合適位置調(diào)用 block
//調(diào)用block if (self.changeMainVCBgColor) { self.changeMainVCBgColor([UIColor redColor]); }
-
在接收者中實(shí)現(xiàn)具體操作
ViewC3 *VC3 = [[ViewC3 alloc] init]; VC3.changeMainVCBgColor = ^(UIColor *color) { self.view.backgroundColor = color; }; [self.navigationController pushViewController:VC3 animated:YES];
-
4. 通知傳值
- 使用場(chǎng)景:代理一般適用于上下級(jí)之間相速,若是多級(jí)結(jié)構(gòu)時(shí)會(huì)相當(dāng)麻煩,使用通知沒有級(jí)別結(jié)構(gòu)限制
- 步驟:
-
在發(fā)送者中發(fā)布通知
//1.發(fā)布通知 NSDictionary *dict = @{@"color":[UIColor grayColor]}; [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeMainBgColor" object:@"VC42" userInfo:dict];
-
在接收者中注冊(cè)通知
//2.一般在 viewDidLoad 中注冊(cè)通知 //通知名稱需一致 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"viewDidLoad" object:@"VC42"];
-
在接收者中實(shí)現(xiàn)通知具體操作
- (void)changeColor:(NSNotification *)noti { NSDictionary *dict = noti.userInfo; self.view.backgroundColor = [dict valueForKeyPath:@"color"]; }
-
在接收者中注銷通知
- (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ChangeMainBgColor" object:@"VC42"]; }
-
5. 單例傳值
- 使用場(chǎng)景:多次創(chuàng)建僅有唯一一個(gè)鲜锚,隨用隨取
- 步驟:
-
創(chuàng)建單例類突诬,聲明需要傳遞的屬性
@interface Person : NSObject interfaceSingleton(PersonName); @property (nonatomic, copy) NSString *name; @end
-
實(shí)例化一個(gè)對(duì)象,對(duì)對(duì)象的屬性進(jìn)行賦值
Person *person = [[Person alloc] init]; person.name = @"單例賦值的名字";
-
在需要用到的地方實(shí)例化一個(gè)對(duì)象芜繁,取出屬性值
Person *person = [[Person alloc] init]; NSLog(@"取出單例賦值:%@", person.name);
PS: 創(chuàng)建單例類請(qǐng)查看我之前的文章《iOS 單例模式簡析》
-
個(gè)人博客:https://mortal-master.github.io