xcode 解雇ViewControllerAnimated 完成块演示者视图和模态视图流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13653048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:13:23  来源:igfitidea点击:

dismissViewControllerAnimated completion block presenter view and modal view flow

objective-cxcodeipad

提问by Patricia

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) 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.

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

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

我知道这就像一个回调,但我仍然不知道把回调的东西放在哪里。

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?

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

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

或者:B) 在模态视图的dismissViewControllerAnimated 完成块中,我是否应该包含在模态视图完成时执行的演示者视图方法?

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

有人可以帮我一些示例代码吗?或者至少可以帮助我了解将代码放入哪个块的流程?

Thank you, P

谢谢你,P

回答by Paul.s

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

您谈论的是完成块,所以我假设您不想使用委托。

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

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

@interface PresentedViewController : UIViewController

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

@end

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

然后在实现中,您需要在解雇时调用此完成块。在这里,我假设 viewController 在单击按钮时被解除

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

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

现在回到呈现模式的 viewController 中,您需要提供实际的完成块 - 通常在您创建 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];
}

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

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

回答by rdelmar

You can do this with delegates, that's the way Apple seems to recommend, but that seems like overkill to me. You have a reference to the presenter with the presentingViewController property, so you can just set the value of a property in the presenter from the presented controller in the button click method:

你可以用代表来做这件事,这就是 Apple 推荐的方式,但这对我来说似乎有点过分了。您可以使用presentingViewController 属性引用演示者,因此您只需在按钮单击方法中从呈现的控制器中设置演示者中的属性值:

self.presentingViewController.someProp = self.theValueToPass;
[self dismissViewControllerAnimated:YES];

回答by Adam Johnson

Using delegates is a good way to handle this:

使用委托是处理这个问题的好方法:

In your PresentedViewController.h

在您的 PresentedViewController.h

@protocol PresentedViewControllerDelegate <NSObject>

-(void) viewWillDismiss;

@end

@property (nonatomic, weak) id <PresentedViewController> delegate;

Then in your PresentingViewController.h, you would subscribe to this delegate

然后在您的 PresentingViewController.h 中,您将订阅此委托

@interface PresentingViewController : UIViewController <PresentedViewControllerDelegate>

in the .m you must implement the delegate method

在 .m 中,您必须实现委托方法

- (void) viewWillDismiss {

}

and before you present the view controller set the delegate property you made as self.

在您呈现视图控制器之前,将您创建的委托属性设置为 self。

presentingViewController.delegate = self;

Obviously not every implementation detail has been done here, but this should get you started.

显然这里并不是所有的实现细节都已经完成,但这应该会让你开始。