iOS 的逆向傳值(從 B 頁把資料送回 A 頁)可用的方式不少,本文把六種常見做法整理出來,只列關鍵程式碼。
最傳統、也最能清楚表達意圖的做法。
第二個控制器:
@protocol WJSecondViewControllerDelegate <NSObject>
- (void)changeText:(NSString *)text;
@end
@property (nonatomic, assign) id<WJSecondViewControllerDelegate> delegate;
- (IBAction)buttonClick:(UIButton *)sender {
_str = sender.titleLabel.text;
[self.delegate changeText:sender.titleLabel.text];
[self.navigationController popViewControllerAnimated:YES];
}
第一個控制器:
- (IBAction)pushToSecond:(id)sender {
WJSecondViewController *svc = [[WJSecondViewController alloc] initWithNibName:@"WJSecondViewController" bundle:nil];
svc.delegate = self;
svc.str = self.navigationItem.title;
[self.navigationController pushViewController:svc animated:YES];
}
- (void)changeText:(NSString *)text {
self.navigationItem.title = text;
}
適合「一對多」廣播,例如多個畫面都關心同一份狀態變動。
第一個控制器(訂閱):
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(limitDataForModel:)
name:@"NOV"
object:nil];
- (void)limitDataForModel:(NSNotification *)noti {
self.gamesInfoArray = noti.object;
}
第二個控制器(發布):
[[NSNotificationCenter defaultCenter] postNotificationName:@"NOV" object:gameArray];
記得在 dealloc 中呼叫 removeObserver:(iOS 9 之前若忘記會直接 crash)。另外通知名稱建議用全域常數而非字串字面值,才不會打錯字還查不出來。
最省事,但也最容易讓資料流向變得難以追蹤。以下假設 Single 是一個單例類別,擁有字串屬性 titleName。
第二個控制器:
- (IBAction)buttonClick:(UIButton *)sender {
Single *single = [Single sharedSingle];
single.titleName = sender.titleLabel.text;
[self.navigationController popViewControllerAnimated:YES];
}
第一個控制器:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
Single *single = [Single sharedSingle];
self.navigationItem.title = single.titleName;
}
回呼意圖明確,程式碼集中,是中小型專案裡相當實用的選擇。
第二個控制器:
@property (nonatomic, copy) void (^changeText_block)(NSString *);
- (IBAction)buttonClick:(UIButton *)sender {
_str = sender.titleLabel.text;
self.changeText_block(sender.titleLabel.text);
[self.navigationController popViewControllerAnimated:YES];
}
第一個控制器:
- (IBAction)pushToSecond:(id)sender {
WJSecondViewController *svc = [[WJSecondViewController alloc] initWithNibName:@"WJSecondViewController" bundle:nil];
svc.str = self.navigationItem.title;
[svc setChangeText_block:^(NSString *str) {
self.navigationItem.title = str;
}];
[self.navigationController pushViewController:svc animated:YES];
}
block 屬性務必宣告為 copy。另外 block 內若強引用 self 而又被 self 持有,就會循環引用,需搭配 __weak 處理。
第二個控制器:
extern NSString *btn;
- (IBAction)buttonClick:(UIButton *)sender {
btn = sender.titleLabel.text;
[self.navigationController popViewControllerAnimated:YES];
}
第一個控制器:
NSString *btn = nil;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationItem.title = btn;
}
第一個控制器:
- (void)viewDidLoad {
[super viewDidLoad];
_vc = [[SecondViewController alloc] init];
// 監聽 vc 的 textValue 屬性
[_vc addObserver:self forKeyPath:@"textValue" options:0 context:nil];
}
第二個控制器:
- (IBAction)buttonClicked:(id)sender {
self.textValue = self.textField.text;
[self.navigationController popViewControllerAnimated:YES];
}
KVO 務必在 dealloc 中移除監聽,否則會出現 observer was not removed 的例外。若要改善 KVO 繁瑣的寫法,可以考慮改用 RAC 或 KeyPath 封裝。
其實還有更多方式,例如 NSUserDefaults(適合少量、輕量的設定值)、寫入 plist 或檔案再讀取等,這裡就不逐一列舉。
實務上的判斷可以這樣想:只是一頁對一頁、單純回傳一個值 → block 或 delegate;多個觀察者關心同一份狀態 → 通知;全域唯一的設定或狀態 → 單例或 NSUserDefaults。而 extern 全域變數雖然寫起來最快,卻最容易造成隱性耦合,除了一兩個小工具性的旗標,不建議在正式專案中當成主要手段。
這些程式碼寫的時間比較久了,今天整理了一下仍略顯零碎,若有不足之處还请見諒。