本文介绍了在prepareForSegue方法中阻止segue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 prepareForSegue:方法中取消segue?



在segue之前,如果条件不为真(在这种情况下,如果某些 UITextField 为空),则显示错误消息而不是执行segue。

解决方案

在iOS 6和更高版本中可能:
您必须实现方法

   - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender 


。如果不是 return NO; 返回YES; > and prepareForSegue is not call。



请注意,当以编程方式触发segues时,此方法不会自动调用,如果您需要执行检查,那么您必须调用shouldPerformSegueWithIdentifier以确定是否执行segue。


Is it possible to cancel a segue in the prepareForSegue: method?

I want to perform some check before the segue, and if the condition is not true (in this case, if some UITextField is empty), display an error message instead of performing the segue.

解决方案

In iOS 6 and later it´s possible: You have to implement the method

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender 

in your view controller. You do your validation there, and if it´s OK then return YES; if it´s not the return NO; and the prepareForSegue is not call.

Note that this method doesn't get called automatically when triggering segues programmatically, if you need to perform the check, then you have to call shouldPerformSegueWithIdentifier to determine whether to perform segue.

这篇关于在prepareForSegue方法中阻止segue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:52