如何检查视图控制器是否可以执行转场

时间:2023-03-23
本文介绍了如何检查视图控制器是否可以执行转场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

这可能是一个非常简单的问题,但在搜索时没有产生任何结果,所以在这里......

This might be a very simple question but didn't yield any results when searching for it so here it is...

我正在尝试找出一种方法来检查某个视图控制器是否可以在调用 performSegueWithIdentifier: 方法之前执行带有标识符 XYZ 的 segue.

I am trying to work out a way to check if a certain view controller can perform a segue with identifier XYZ before calling the performSegueWithIdentifier: method.

类似的东西:

if ([self canPerformSegueWithIdentifier:@"SegueID"])
    [self performSegueWithIdentifier:@"SegueID"];

可能吗?

推荐答案

如文档所述:

应用程序通常不需要直接触发 segue.相反,您在 Interface Builder 中配置一个与视图控制器,例如嵌入在其视图层次结构中的控件,触发segue.但是,您可以调用此方法来触发以编程方式进行 segue,也许是为了响应某些无法执行的操作在情节提要资源文件中指定.例如,您可能从用于处理抖动的自定义操作处理程序调用它或加速度计事件.

Apps normally do not need to trigger segues directly. Instead, you configure an object in Interface Builder associated with the view controller, such as a control embedded in its view hierarchy, to trigger the segue. However, you can call this method to trigger a segue programmatically, perhaps in response to some action that cannot be specified in the storyboard resource file. For example, you might call it from a custom action handler used to process shake or accelerometer events.

收到此消息的视图控制器必须已加载从故事板.如果视图控制器没有关联的故事板,也许是因为你自己分配和初始化了它,此方法抛出异常.

The view controller that receives this message must have been loaded from a storyboard. If the view controller does not have an associated storyboard, perhaps because you allocated and initialized it yourself, this method throws an exception.

话虽如此,当您触发 segue 时,通常是因为假设 UIViewController 将能够使用特定的 segue's 标识符.我也同意 Dan F,您应该尽量避免可能引发异常的情况.作为你不能做这样的事情的原因:

That being said, when you trigger the segue, normally it's because it's assumed that the UIViewController will be able to respond to it with a specific segue's identifier. I also agree with Dan F, you should try to avoid situations where an exception could be thrown. As the reason for you not to be able to do something like this:

if ([self canPerformSegueWithIdentifier:@"SegueID"])
    [self performSegueWithIdentifier:@"SegueID"];

我猜:

  1. respondsToSelector: 仅检查您是否能够在运行时处理该消息.在这种情况下你可以,因为类 UIViewController 能够响应 performSegueWithIdentifier:sender:.要实际检查一个方法是否能够处理带有某些参数的消息,我想这是不可能的,因为为了确定是否有可能它必须实际运行它,并且在这样做时 NSInvalidArgumentException会上升.
  2. 要实际创建您建议的内容,接收 UIViewController 关联的 segue id 列表会很有帮助.从 UIViewController 文档,我找不到任何类似的东西
  1. respondsToSelector: only checks if you are able to handle that message in runtime. In this case you can, because the class UIViewController is able to respond to performSegueWithIdentifier:sender:. To actually check if a method is able to handle a message with certain parameters, I guess it would be impossible, because in order to determine if it's possible it has to actually run it and when doing that the NSInvalidArgumentException will rise.
  2. To actually create what you suggested, it would be helpful to receive a list of segue's id that the UIViewController is associated with. From the UIViewController documentation, I wasn't able to find anything that looks like that

就目前而言,我猜你最好的选择是继续使用 @try @catch @finally.

As for now, I am guessing your best bet it's to keep going with the @try @catch @finally.

这篇关于如何检查视图控制器是否可以执行转场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:iOS - 如何创建“实用程序"?我的所有控制器都可以调用的类 下一篇:iOS:UISplitViewController 无法推送到 UINavigationController

相关文章