問題描述
這可能是一個非常簡單的問題,但在搜索時沒有產(chǎn)生任何結(jié)果,所以在這里......
This might be a very simple question but didn't yield any results when searching for it so here it is...
我正在嘗試找出一種方法來檢查某個視圖控制器是否可以在調(diào)用 performSegueWithIdentifier:
方法之前執(zhí)行帶有標識符 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"];
可能嗎?
推薦答案
如文檔所述:
應用程序通常不需要直接觸發(fā) segue.相反,您在 Interface Builder 中配置一個與視圖控制器,例如嵌入在其視圖層次結(jié)構(gòu)中的控件,觸發(fā)segue.但是,您可以調(diào)用此方法來觸發(fā)以編程方式進行 segue,也許是為了響應某些無法執(zhí)行的操作在情節(jié)提要資源文件中指定.例如,您可能從用于處理抖動的自定義操作處理程序調(diào)用它或加速度計事件.
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.
收到此消息的視圖控制器必須已加載從故事板.如果視圖控制器沒有關(guān)聯(lián)的故事板,也許是因為你自己分配和初始化了它,此方法拋出異常.
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.
話雖如此,當您觸發(fā) segue
時,通常是因為假設 UIViewController
將能夠使用特定的 segue's
標識符.我也同意 Dan F,您應該盡量避免可能引發(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"];
我猜:
respondsToSelector:
僅檢查您是否能夠在運行時處理該消息.在這種情況下你可以,因為類UIViewController
能夠響應performSegueWithIdentifier:sender:
.要實際檢查一個方法是否能夠處理帶有某些參數(shù)的消息,我想這是不可能的,因為為了確定是否有可能它必須實際運行它,并且在這樣做時NSInvalidArgumentException
會上升.- 要實際創(chuàng)建您建議的內(nèi)容,接收
UIViewController
關(guān)聯(lián)的 segue id 列表會很有幫助.從UIViewController
文檔,我找不到任何類似的東西
respondsToSelector:
only checks if you are able to handle that message in runtime. In this case you can, because the classUIViewController
is able to respond toperformSegueWithIdentifier: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 theNSInvalidArgumentException
will rise.- 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 theUIViewController
documentation, I wasn't able to find anything that looks like that
就目前而言,我猜你最好的選擇是繼續(xù)使用 @try
@catch
@finally
.
As for now, I am guessing your best bet it's to keep going with the @try
@catch
@finally
.
這篇關(guān)于如何檢查視圖控制器是否可以執(zhí)行轉(zhuǎn)場的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!