頁面之間傳資料是所有 App 都會遇到的基本需求。最直覺的做法是屬性傳值,適合「A 傳給 B」;而當你需要「B 把結果送回 A」,Block 回呼會更乾淨。這篇用同一組範例把兩種方式都跑一遍。
目標是把 FirstViewController 文字欄位的內容,帶到 SecondViewController 的導覽列標題上。
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController {
UITextField *tf;
}
@end
#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];
}
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@property (nonatomic, strong) NSString *str;
@property (nonatomic, copy) NSString *naviTitle;
@end
#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
反過來,讓 SecondViewController 把輸入框的內容送回第一頁並顯示在 UILabel 上。
#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
#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
#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 屬性務必用 copy 修飾;若 Block 內強引用 self 而 self 又持有該 Block,會形成循環引用,必要時使用 __weak 打破迴圈。