问题描述
是否可以在 prepareForSegue:
方法中取消 segue?
Is it possible to cancel a segue in the prepareForSegue:
method?
我想在 segue 之前执行一些检查,如果条件不成立(在这种情况下,如果某些 UITextField
为空),则显示错误消息而不是执行 segue.
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.
推荐答案
在 iOS 6 及更高版本中是可能的:你必须实现方法
It's possible in iOS 6 and later:You have to implement the method
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
在您的视图控制器中.您在那里进行验证,如果可以,则 return YES;
如果不是,则 return NO;
并且不调用 prepareForSegue.
In your view controller. You do your validation there, and if it's OK then return YES;
if it's not then return NO;
and the prepareForSegue is not called.
请注意,以编程方式触发 segue 时,不会自动调用此方法.如果需要进行检查,则需要调用shouldPerformSegueWithIdentifier来判断是否进行segue.
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!