Iphone 停止segue并显示警报
使用iOS 5情节串连板,在我正在执行segue的按钮上,我想对我的文本字段进行验证,如果验证失败,我必须停止segue并发出警报。怎么做?如果您的部署目标是iOS 6.0或更高版本Iphone 停止segue并显示警报,iphone,ios,ios5,segue,uistoryboardsegue,Iphone,Ios,Ios5,Segue,Uistoryboardsegue,使用iOS 5情节串连板,在我正在执行segue的按钮上,我想对我的文本字段进行验证,如果验证失败,我必须停止segue并发出警报。怎么做?如果您的部署目标是iOS 6.0或更高版本 您只需在源代码视图控制器上实现shouldPerformSegueWithIdentifier:sender:方法。如果要执行此步骤,请使此方法返回YES;如果不想执行此步骤,请使此方法返回NO 如果您的部署目标早于iOS 6.0 您需要更改序列图像在情节提要中的连接方式,并编写更多的代码 首先,设置从按钮的视图控
您只需在源代码视图控制器上实现
shouldPerformSegueWithIdentifier:sender:
方法。如果要执行此步骤,请使此方法返回YES
;如果不想执行此步骤,请使此方法返回NO
如果您的部署目标早于iOS 6.0
您需要更改序列图像在情节提要中的连接方式,并编写更多的代码
首先,设置从按钮的视图控制器到目标视图控制器的顺序,而不是直接从按钮到目标。为序列指定一个标识符,如validationsucceed
然后,将按钮连接到其视图控制器上的操作。在操作中,执行验证,然后根据验证是否成功执行segue或显示警报。它看起来像这样:
- (IBAction)performSegueIfValid:(id)sender {
if ([self validationIsSuccessful]) {
[self performSegueWithIdentifier:@"ValidationSucceeded" sender:self];
} else {
[self showAlertForValidationFailure];
}
}
对于我来说,我认为正确的答案是使用UIViewController方法,该方法可以在:
shouldPerformSegueWithIdentifier:发件人: 我是这样实现我的方法的:
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
if ([identifier isEqualToString:@"Identifier Of Segue Under Scrutiny"]) {
// perform your computation to determine whether segue should occur
BOOL segueShouldOccur = YES|NO; // you determine this
if (!segueShouldOccur) {
UIAlertView *notPermitted = [[UIAlertView alloc]
initWithTitle:@"Alert"
message:@"Segue not permitted (better message here)"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
// shows alert to user
[notPermitted show];
// prevent segue from occurring
return NO;
}
}
// by default perform the segue transition
return YES;
}
工作得很有魅力
更新为Swift for>=iOS 8:
override func shouldPerformSegueWithIdentifier(identifier: String!, sender: AnyObject!) -> Bool {
if identifier == "Identifier Of Segue Under Scrutiny" {
// perform your computation to determine whether segue should occur
let segueShouldOccur = true || false // you determine this
if !segueShouldOccur {
let notPermitted = UIAlertView(title: "Alert", message: "Segue not permitted (better message here)", delegate: nil, cancelButtonTitle: "OK")
// shows alert to user
notPermitted.show()
// prevent segue from occurring
return false
}
}
// by default perform the segue transitio
return true
}
我给你举个例子,下面是我的代码:
- (IBAction)Authentificate:(id)sender {
if([self WSAuthentification]){
[self performSegueWithIdentifier:@"authentificationSegue" sender:sender];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Authetification Failed" message:@"Please check your Identifications" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
[alert show];
}
但这似乎不起作用,在所有情况下,我的segue都被执行了。
答案很简单,我们必须从视图控制器连接segue,而不是从按钮连接segue。PerformsgueIfValid()的使用有任何缺点吗?我相信这是一种用户定义的方法(在文档中没有找到)…所以我没有尝试。但在我看来,从设计角度来看,使用故事板分段并不理想,而是将连接从其典型插座重定向以适应功能。ShouldPerformanceSegueWithIdentifier:sender:从iOS 6开始提供,并且不会从iOS 5调用。适用于新手。要在interface builder中查找segue标识符,请打开情节提要->选择场景(左侧),选择场景中的segue(左侧),然后在右侧选择属性检查器。使用标识符字段。这是更好的解决方案-应位于顶部。文档中没有该字段,但似乎PerformsgueWithIdentifier:sender:不调用shouldPerformSegueWithIdentifier:sender:。因此,如果您是以编程方式触发segue,那么在执行此操作之前,还应该以编程方式检查shouldPerformSegueWithIdentifier:sender:。