0

我通过以下代码从视图控制器 A 转到 B:

[self presentViewController:B animated:YES completion:^{
    }];

当视图 B 被关闭时如何通知我,此时我如何将一些参数从 B 回传到 A?

4

3 回答 3

2

当需要关闭 View Controller B 时,请使用以下方法:

// This code goes inside View Controller B. Here "self.presentingViewController"
// refers to View Controller A
[self.presentingViewController dismissViewControllerAnimated:YES completion:^{
    //completion code here
}];

// In your View Controller A implement dismissViewControllerAnimated:completion:
// and call super. This code goes in View Controller A
-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion {
    // Here you can add your custom code. 
    [super dismissViewControllerAnimated:flag completion:completion];
}

这样视图控制器 A 将知道视图控制器 B 何时被解雇。

希望这可以帮助!

于 2013-08-27T18:55:17.983 回答
1

您应该使用@protocol. 如果您查看 Apple 的指导,您会看到这种模式在许多提供的视图控制器中重复出现,例如UIImagePickerController告诉UIImagePickerControllerDelegate委托imagePickerController:didFinishPickingMediaWithInfo:

于 2013-08-27T18:52:18.247 回答
1

创建一个协议并让视图控制器 A 实现它。现在,在视图控制器 B 中创建一个属性委托并将其委托设置为 A。然后,当您要关闭时,请在委托(即 A)上调用适当的协议方法,以便 A 知道 B 已准备好被关闭,因此A 在解雇 B 之前完成其任务,然后解雇 B。

您还可以使用 presentingViewController 从 B 访问 A 或使用 UIViewController 的 presentViewController 属性从 A 访问 B。

于 2013-08-27T18:53:15.003 回答