根視圖 ViewController 以 presentViewController 彈出 EventViewController,在後者中需返回根視圖。
// 在 EventViewController 中
self.vc = [[ViewController alloc] init]; // 重新建立實例!錯誤
[self presentViewController:self.vc animated:YES completion:nil]; // 再彈一層
// 在 EventViewController 的按鈕動作中
- (IBAction)iconAction {
[self dismissViewControllerAnimated:YES completion:nil];
}
presentViewController 建立模態堆疊new 根視圖控制器,那會建立新實例導致狀態丟失使用 Delegate 代理模式 或 Block 回調:
// EventViewController.h
@property (weak) id delegate;
// 關閉前
[self.delegate eventDidFinishWithData:data];
[self dismissViewControllerAnimated:YES completion:nil];