iOS Block傳值、代理傳值、通知中心

在我們需要在另一個頁面中獲取到這個頁面個的數據的時候,我們就需要將這個頁面通過某種形式傳遞給另一個頁面。在這里我們將這兩個頁面分別稱為,傳值頁面和接收頁面。今天介紹的傳值的方式多用于從后往前傳值。

Block傳值

Block傳值的兩種方式

- 使用Block屬性實現回調傳值

在Block傳值中傳值頁面個接收頁面兩者之間的關系是,在接收頁面我們在點擊Button按鈕的時候,導航控制器推出傳值頁面,在傳值的頁面點擊Button按鈕時會pop出接收頁面,Block傳值的過程就在這里完成。

  • 第一步在我們的傳值的頁面聲明一個Block屬性
#import <UIKit/UIKit.h>
typedef void(^sendValue)(NSString *value);
@interface SecondViewController : UIViewController
@property (nonatomic, copy)sendValue sendValueBlock;
@end
  • 第二步在我們的傳值頁面里需要傳值的地方調用Block方法
    這里是將傳值頁面中的textField的傳遞給了接收的頁面
#import "SecondViewController.h"
@implementation SecondViewController
-(void)buckAction:(UIBarButtonItem *)back{
    self.sendValue(self.textField.text);
    [self.navigationController popViewControllerAnimated:YES];
}
@end
  • 在接收值得頁面里實現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];
}
@end

- 在方法中定義Block實現回調傳值

  • 第一步在AppTool.h中重定義void(^)(NSString *string)類型名別為AppToolBlock
AppTool.h
typedef void(^AppToolBlock)(NSString *string);
  • 第二步聲明方法,在方法中封裝Block
AppTool.h
-(void)sendNumber:(NSInteger )number andBlock:(AppToolBlock)block;
  • 第三步在AppTool.m實現方法,并執行Block
-(void)sendNumber:(NSInteger )number andBlock:(AppToolBlock)block{
 NSString *string = [NSStringstringWithFormat:@"%ld",number];
 block(string);
}
  • 第四步在需要接收值得地方調用該方法
#import "SecondViewController.h"
@implementation SecondViewController
AppTool *appTool = [[AppTool alloc] init];
[appTool sendNumber:216541 andBlock:^(NSString*string) { 
self.label.text = string;
}];
@end

至此Block的兩種傳值的方法已經介紹完畢,歡迎指正。

協議傳值

協議傳值共分為六步

  • 第一步聲明協議,聲明協議方法
#import <UIKit/UIKit.h>
//協議名的命名規范:類名+delegate
@protocol SecondDelegate <NSObject>
//傳值的內容作為協議方法的參數
-(void)passString:(NSString *)string;
@end
  • 第二步添加代理人屬性
@interface SecondViewController : UIViewController
@property(nonatomic,weak)id<SecondDelegate>delegate;
//用assign,weak是為了防止產生保留環,造成無法釋放
@end
  • 第三步讓代理人執行協議方法
#import "SecondViewController.h"
-(void)buckAction:(UIBarButtonItem *)back{
    if (self.delegate && [self.delegate respondsToSelector:@selector(passString:)]) {
//判斷代理人是否存在,和是否實現了代理方法
        [self.delegate passString:self.textField.text];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
@end
  • 第四步簽訂協議
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()<SecondDelegate>
@end
  • 第五步指定代理人
#import "FirstViewController.h"
-(void)buttonAction:(UIButton *)button{
    SecondViewController *secondVC = [[SecondViewController alloc]init];
    secondVC.delegate = self;
    [self.navigationController pushViewController:secondVC animated:YES];
}
@end
  • 第六步實現協議方法
#import "FirstViewController.h"
-(void)passString:(NSString *)string{
    self.label.text = string;
}
@end

至此協議傳值的方法已經介紹完畢,歡迎指正。

通知中心傳值

首先我們來了解我們需要用到的幾個相關類

  • NSNotificationCenter
    這個類是一個通知中心,使用單例設計,每個應用程序都會有一個默認的通知中心。用于監聽某條廣播。
  • NSNotification
    這個類可以理解為一個消息對象,其中有三個成員變量。
@property (readonly, copy) NSString *name;//這個成員變量是這個消息對象的唯一標識,用于辨別消息對象。
@property (readonly, retain) id object;//這個成員變量定義一個對象,可以理解為針對某一個對象的消息。
@property (readonly, copy) NSDictionary *userInfo;這個成員變量是一個字典,可以用其來進行傳值。

示例代碼

  • 給接收值的頁面注冊一個消息通知
    注意:
    observer: 觀察者
    收到廣播時候的回調方法,可以不帶參數,也可以帶參數,如果帶參數,參數的類型為廣播類型(NSNotification)
    name:廣播的名稱
    object:如果發送的通知指定了object對象,那么觀察者接收的通知設置的object對象與其一樣,才會接收到通知,但是接收通知如果將這個參數設置為了nil,則會接收一切通知。
#import "RootViewController.h"
@implementation RootViewController
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotifi:) name:@"test" object:nil];
@end
  • receiveNotifi方法的實現
#import "RootViewController.h"
@implementation RootViewController
-(void)receiveNotifi:(NSNotification *)notifi{
   NSLog(@"object=====%@",notifi.object);
    NSLog(@"userInfo=====%@",notifi.userInfo);
}
@end
  • 第二步在要傳值出去的界面發送通知消息
#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
  • 注意:在用通知中心傳值的時候,需要保證參數中的廣播名稱是一致的。
    至此通知中心傳值的方法已經介紹完畢,歡迎指正。
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容