【问题标题】:Dismiss ViewController that is presented via Popup关闭通过 Popup 呈现的 ViewController
【发布时间】:2013-02-06 17:06:56
【问题描述】:

所以我正在使用情节提要创建一个基于 ipad 食谱的应用程序。只是为了让您了解我的程序的结构,这里是 View 控制器的流程:

ViewController --Modally--> PreviewViewController --Modally--> RecipeViewController --Popup--> IngredientsViewController

所有这些都在故事板中完成。我创建了IngredientsViewController 并将其链接到RecipeViewController 以显示为弹出窗口,效果很好。但是我希望能够以编程方式关闭IngredientsViewController(因为我已经实现了语音命令功能)。问题是我似乎无法访问 IngredientsViewController 以将其关闭。 (抱歉,我还不能发布任何图片)。

我正在使用以下代码以编程方式呈现IngredientsViewController(来自RecipeViewController):

[self performSegueWithIdentifier:@"ingr" sender:nil];

显示弹出窗口效果很好,但我无法关闭它。我试图通过视图控制器的层次结构访问它,但似乎找不到它,我假设它位于堆栈的顶部,但显然不是?我试过这样的事情:

[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];

这又是从RecipeViewController 中调用的。但这只是忽略了RecipeViewController,而不是弹出窗口显示的IngredientsViewController

要明确IngredientsViewController 不是UIPopoverController,它是在故事板中创建的普通viewController,其segue 样式是弹出链接到RecipeViewController 中的按钮。

任何帮助将不胜感激。谢谢!

【问题讨论】:

  • IngredientsViewController 中有任何按钮可以关闭吗?
  • 不,我想从 RecipeViewController 中将其关闭,我使用语音控制来关闭它(但这都不是问题,它只是关闭它的实际方法。

标签: ios xcode ipad popup uipopovercontroller


【解决方案1】:
UIPopoverController *popOver = (UIPopoverController *)self.presentedViewController;
[popOver dismissPopoverAnimated:YES];

如果您的目标视图控制器是UIPopoverController,这应该可以解决问题

【讨论】:

  • 我的目标 ViewController 不是 UIPopoverController,它是一个普通的 viewController,但谢谢我可能会考虑将其设为 UIPopoverController。
  • 最后我决定把它变成一个 UIPopoverController 然后在弹出窗口中实例化 ViewController 。因此,在同一个 ViewController 中呈现和关闭它很简单。
【解决方案2】:

您是否尝试过presentViewController:animated:completion: 而不是performSegueWithIdentifier:

【讨论】:

  • 我刚刚使用以下代码尝试了这种方法:UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"IngrCtr"]; [self presentViewController: vc animated:YES completion:nil]; 它有点工作,但不是将它呈现为我定义大小的弹出框,它只是将它呈现为覆盖我的全屏前一个视图控制器。
  • 我意识到为什么我使用 performSegueWithIdentifier,这是因为我通过那个 segue 传递信息,所以我需要坚持以该方法呈现它,或者找到另一种传递信息的方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-15
  • 2016-06-02
  • 2020-04-27
  • 1970-01-01
相关资源
最近更新 更多