有的時(shí)候,我們需要頁(yè)面跳轉(zhuǎn)傳遞數(shù)據(jù),就需要將這個(gè)頁(yè)面通過(guò)某種形式傳遞給另一個(gè)頁(yè)面。我們把兩個(gè)頁(yè)面分別記做:傳值頁(yè)面 接受頁(yè)面
某種形式傳遞包括:Block, 代理(delegate), 通知(Notification)
誰(shuí)傳值誰(shuí)就設(shè)置代理 誰(shuí)傳值誰(shuí)就設(shè)置Block
Block傳值
1.使用Block屬性實(shí)現(xiàn)回調(diào)傳值
- 在傳值頁(yè)面聲明一個(gè)Block屬性
typedef void(^sendValue)(NSString *context);
@interface SecondViewController : UIViewController
@property (copy,nonatomic) sendValue sendValueBlock;
- 我們的傳值頁(yè)面里需要傳值的地方調(diào)用Block方法這里是將傳值頁(yè)面中的textField的傳遞給了接收的頁(yè)面
- (IBAction)sendButton:(UIButton *)sender
{
self.sendValueBlock(self.textFile.text);
[self.navigationController popViewControllerAnimated:YES];
}
- 在接收值得頁(yè)面里實(shí)現(xiàn)Block
#import "FirstViewController.h"
@implementation FirstViewController
-(void)buttonAction:(UIButton *)button
{
SecondViewController *secondVC = [[SecondViewController alloc]init];
__weak FirstViewController *firstVC = self;
secondVC.sendValue = ^(NSString *str){
firstVC.label .text = str;
};
[self.navigationController pushViewController:secondVC animated:YES];
}
2.方法中定義Block實(shí)現(xiàn)回調(diào)傳值
- 在傳值頁(yè)面 .h 文件申明Block屬性
typedef void(^sendValue)(NSString *context);
- 在傳值頁(yè)面 .h 文件聲明Block方法
-(void)sendString:(NSString *)string andBlock: (sendValue)block;
- 在 傳值頁(yè)面.m文件實(shí)現(xiàn)方法
-(void)sendString:(NSString *)string andBlock: (sendValue)block
{
block(string);
}
- 在接受頁(yè)面實(shí)現(xiàn)方法
SecondViewController *secondVC = [[SecondViewController alloc]init];
[secondVC sendString:(NSString *)string andBlock:^(NSString*string) {
self.label.text = string;
}
];
block傳值兩種方法 有問(wèn)題歡迎指正
2.代理傳值
- 第一步聲明協(xié)議,聲明協(xié)議方法
//設(shè)置代理 協(xié)議名的命名規(guī)范:類名+delegate
@protocol sendValueDelegate <NSObject>
//傳值的內(nèi)容作為協(xié)議方法的參數(shù)
- (void)sendString:(NSString *)string;
@end
- 第二步添加代理人屬性
@interface SecondViewController : UIViewController
@property (weak,nonatomic) id<sendValueDelegate> delegate;
- 第三步讓代理人執(zhí)行協(xié)議方法
- (IBAction)backButton:(UIButton *)sender {
//判斷代理人是否存在,和是否實(shí)現(xiàn)了代理方法
if (self.delegate && [self.delegate respondsToSelector:@selector(sendString:)]) {
[self.delegate sendString:self.textFile.text];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
- 第四步簽訂協(xié)議
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()<sendValueDelegate>
@end
- 第五步指定代理人
#import "FirstViewController.h"
-(void)buttonAction:(UIButton *)button
{
SecondViewController *secondVC = [[SecondViewController alloc]init];
secondVC.delegate = self;
[self.navigationController pushViewController:secondVC animated:YES];
}
@end
- 第六步實(shí)現(xiàn)協(xié)議方法
#import "FirstViewController.h"
-(void)sendString:(NSString *)string
{
self.label.text = string;
}
@end
通知中心傳值
- NSNotificationCenter這個(gè)類是一個(gè)通知中心,使用單例設(shè)計(jì),每個(gè)應(yīng)用程序都會(huì)有一個(gè)默認(rèn)的通知中心。用于監(jiān)聽(tīng)某條廣播。
- NSNotification這個(gè)類可以理解為一個(gè)消息對(duì)象,其中有三個(gè)成員變量。
@property (readonly, copy) NSString *name;//這個(gè)成員變量是這個(gè)消息對(duì)象的唯一標(biāo)識(shí),用于辨別消息對(duì)象。
@property (readonly, retain) id object;//這個(gè)成員變量定義一個(gè)對(duì)象,可以理解為針對(duì)某一個(gè)對(duì)象的消息。
@property (readonly, copy) NSDictionary *userInfo;這個(gè)成員變量是一個(gè)字典,可以用其來(lái)進(jìn)行傳值。
示例代碼
給接收值的頁(yè)面注冊(cè)一個(gè)消息通知注意:
- observer: 觀察者收到廣播時(shí)候的回調(diào)方法,可以不帶參數(shù),也可以帶參數(shù),如果帶參數(shù),參數(shù)的類型為廣播類型(NSNotification)
- name:廣播的名稱object:如果發(fā)送的通知指定了
- object對(duì)象,那么觀察者接收的通知設(shè)置的object對(duì)象與其一樣,才會(huì)接收到通知,但是接收通知如果將這個(gè)參數(shù)設(shè)置為了nil,則會(huì)接收一切通知。
#import "RootViewController.h"
@implementation RootViewController
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotifi:) name:@"test" object:nil];
@end
receiveNotifi方法的實(shí)現(xiàn)
#import "RootViewController.h"
@implementation RootViewController
-(void)receiveNotifi:(NSNotification *)notifi
{
NSLog(@"object=====%@",notifi.object);
NSLog(@"userInfo=====%@",notifi.userInfo);
}
@end
第二步在要傳值出去的界面發(fā)送通知消息
#import "SecondViewController.h"
@implementation SecondViewController
-(IBAction)button:(id)sender {
[[NSNotificationCenter defaultCenter]postNotificationName:@"test" object:self.myTextField userInfo:@{@"title":self.myTextField.text}];
[self.navigationController popViewControllerAnimated:YES];
}
@end
文/Joker_King(簡(jiǎn)書(shū)作者)原文鏈接:http://www.lxweimin.com/p/1b4d69e6cb4a著作權(quán)歸作者所有,轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),并標(biāo)注“簡(jiǎn)書(shū)作者”。