A 控制器 present B 控制器,B 輸入資料後需回傳給 A。嘗試用共享模型物件 test *t 但 A 收不到更新。
B 中 Example3ViewController *e3v = [[Example3ViewController alloc] init...] **建立了新實例**,並非原本 present A 的實例。A 的 viewWillAppear 不會被再次調用。
// B 控制器定義協議
@protocol EditorViewControllerDelegate
- (void)editorDidFinishWithData:(Test *)data;
@end
@interface EditorViewController : UIViewController
@property (weak) id delegate;
@end
// B 完成時
- (IBAction)done {
Test *t = [[Test alloc] init];
t.name = self.vtitle.text; ...
[self.delegate editorDidFinishWithData:t];
[self dismissViewControllerAnimated:YES completion:nil];
}
// A 實現協議
- (void)editorDidFinishWithData:(Test *)data {
self.b = data;
self.labelname.text = data.name; ...
}
// A present B 時設定代理
EditorViewController *editor = [[EditorViewController alloc] init...];
editor.delegate = self;
[self presentViewController:editor animated:YES completion:nil];
// B 定義
@property (copy) void (^completionBlock)(Test *data);
// A 使用
editor.completionBlock = ^(Test *data) {
self.b = data;
[self updateUI];
};
viewWillAppear **不會再次調用**