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

joker_king發表於2018-12-19

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

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
複製程式碼
  • 注意:在用通知中心傳值的時候,需要保證引數中的廣播名稱是一致的。 至此通知中心傳值的方法已經介紹完畢,歡迎指正。

相關文章