您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 兩個 ViewController 之間傳值:屬性傳值與 Block 回呼叫法

編輯:iOS 開發綜合

頁面之間傳資料是所有 App 都會遇到的基本需求。最直覺的做法是屬性傳值,適合「A 傳給 B」;而當你需要「B 把結果送回 A」,Block 回呼會更乾淨。這篇用同一組範例把兩種方式都跑一遍。

一、屬性傳值:Forward

目標是把 FirstViewController 文字欄位的內容,帶到 SecondViewController 的導覽列標題上。

來源頁 FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController {
    UITextField *tf;
}
@end

來源頁 FirstViewController.m

#import "FirstViewController.h"
// 要把資料送到哪一個頁面,就匯入哪一個頁面的標頭檔
#import "SecondViewController.h"

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(100, 100, 100, 100);
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self
               action:@selector(doButton)
     forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

    tf = [[UITextField alloc] initWithFrame:CGRectMake(10, 300, 100, 40)];
    tf.tintColor = [UIColor orangeColor];
    tf.backgroundColor = [UIColor grayColor];
    tf.tag = 1000;
    [self.view addSubview:tf];
}

- (void)doButton {
    tf = (UITextField *)[self.view viewWithTag:1000];

    // 一定要先建立實例,否則找不到對應的屬性
    SecondViewController *seV = [[SecondViewController alloc] init];

    // 賦值必須寫在 push 之前
    seV.naviTitle = tf.text;
    seV.str = @"傳值成功";

    [self.navigationController pushViewController:seV animated:YES];
}

目標頁 SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController
@property (nonatomic, strong) NSString *str;
@property (nonatomic, copy) NSString *naviTitle;
@end

目標頁 SecondViewController.m

#import "SecondViewController.h"

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    // 把上一頁傳來的內容顯示在標題上
    self.title = self.naviTitle;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"%@", self.str);
}

@end

二、Block 傳值:Backward

反過來,讓 SecondViewController 把輸入框的內容送回第一頁並顯示在 UILabel 上。

來源頁 FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(100, 100, 100, 100);
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(doButton)
     forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

    // 用來顯示回傳結果
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 400, 100, 40)];
    self.label.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.label];
}

- (void)doButton {
    SecondViewController *seV = [[SecondViewController alloc] init];

    // 傳入一個 Block,等對方回呼時再把資料填進 label
    [seV returnText:^(NSString *showText) {
        self.label.text = showText;
    }];

    [self.navigationController pushViewController:seV animated:YES];
}

@end

目標頁 SecondViewController.h

#import <UIKit/UIKit.h>

// 先定義型別,讓宣告更乾淨
typedef void (^ReturnTextBlock)(NSString *showText);

@interface SecondViewController : UIViewController
@property (nonatomic, strong) UITextField *tf;
@property (nonatomic, copy) ReturnTextBlock returnTextBlock;
- (void)returnText:(ReturnTextBlock)block;
@end

目標頁 SecondViewController.m

#import "SecondViewController.h"

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tf = [[UITextField alloc] initWithFrame:CGRectMake(10, 300, 100, 40)];
    self.tf.tintColor = [UIColor orangeColor];
    self.tf.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.tf];
}

// 把來源頁傳進來的 Block 保存起來,等待時機呼叫
- (void)returnText:(ReturnTextBlock)block {
    self.returnTextBlock = block;
}

// 時機就是「本頁即將消失」
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.returnTextBlock != nil) {
        self.returnTextBlock(self.tf.text);
    }
}

@end

該選哪一種?

  • 屬性傳值:適合正向、一次性的資料傳遞,寫法最簡單。
  • Block 回呼:適合回傳使用者操作結果,也常用於網路請求完成後的處理。
  • Delegate 協議:當回呼資訊較多、或需要多個方法時,比 Block 更易讀。
  • NSNotification:跨越多層、發送者與接收者不需要互相認識時使用。

實務上要注意兩點:Block 屬性務必用 copy 修飾;若 Block 內強引用 self 而 self 又持有該 Block,會形成循環引用,必要時使用 __weak 打破迴圈。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved