dismissViewControllerAnimated完成块演示者视图和模态视图流 [英] dismissViewControllerAnimated completion block presenter view and modal view flow

查看:57
本文介绍了dismissViewControllerAnimated完成块演示者视图和模态视图流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实找到了这个标题的答案,我做了一些研究,但我仍然没有得到这个流程。以下是我想要发生的事情:

I did find an answer to this title and I did do a little research but I'm still not getting the flow. Here is what I want to happen:

1)单击演示者视图上的按钮以打开模态视图。 2)检索一些值,然后单击一个按钮关闭模态视图....将值发送到presentor视图并执行一个方法。

1) click a button on the presenter view to open a modal view. 2) retrieve some value and click a button to close the modal view....sending the value to the presentor view and execute a method.

我知道这是有效的就像一个回调,但我仍然无法弄清楚回调的东西在哪里。

I get that this works like a callback but I still can't figure out where to put the callback stuff.

那么,我该怎么做呢? A)在presentViewController完成块中,我应该在模态视图完成时包含要执行的演示者视图方法吗?

So, how exactly do I do this? A) In the presentViewController completion block, should I include the presenter view method to execute when modal view is completed?

或者:B)在模态视图的dismissViewControllerAnimated完成块中,我应该在模态视图完成时包含presenter视图方法吗?

Or: B) In the modal view's dismissViewControllerAnimated completion block, should I include the presenter view method to execute when modal view is completed?

有人可以帮我提供一些示例代码吗?或者至少帮我获取代码放入哪个块的流程?

Can somebody help me with some sample code? Or at least help me get the flow of which block to put the code in?

谢谢你,P

推荐答案

你谈到完成块,所以我假设你不想使用代理。

You talk about completion blocks so I am assuming you do not want to use delegates.

在将要呈现的viewController中在模态上,你需要提供一个公共完成处理程序,它将在被解雇时被调用。

In the viewController that will be presented modally you need to provide a public completion handler, that will be called when it is dismissed.

@interface PresentedViewController : UIViewController

@property (nonatomic, strong) void (^onCompletion)(id result);

@end

然后在实现中你需要调用这个完成块解雇这里我假设viewController在点击按钮时被解除

Then in the implementation you need to call this completion block on dismissal. Here I assume the viewController is dismissed on a button click

- (IBAction)done:(id)sender
{
  if (self.onCompletion) {
    self.onCompletion(self.someRetrievedValue);
  }
}

现在回到提供你需要的模态的viewController中提供实际的完成块 - 通常在您创建viewController时

Now back in the viewController that presented the modal you need to provide the actual completion block - normally when you create the viewController

- (IBAction)showModal;
{
  PresentedViewController *controller = [[PresentedViewController alloc] init];
  controller.onCompletion = ^(id result) {
    [self doSomethingWithTheResult:result]
    [self dismissViewControllerAnimated:YES completion:nil];
  }
  [self presentViewController:controller animated:YES completion:nil];
}

这将创建新的viewController,以模态方式呈现并定义需要发生的事情完成后。

This will create the new viewController to be presented modally and define what needs to happen on completion.

这篇关于dismissViewControllerAnimated完成块演示者视图和模态视图流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆