如何检查视图控制器是否可以执行segue [英] How to check if a view controller can perform a segue

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

问题描述

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

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,可能是为了响应在storyboard资源文件中无法指定
的某些操作。例如,你可以用
从用于处理shake或
加速度计事件的自定义动作处理程序中调用它。

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的标识符对其进行回复。我也同意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 与...相关联。来自 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 @终于

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

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

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