ios 停止转场并显示警报
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9407571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
To stop segue and show alert
提问by Firdous
Using iOS 5 storyboards, on a button I am performing a segue, what I want is to do validation on my textfield and if validation is failed I have to stop segue and throw an alert. Whats the way to do it?
使用 iOS 5 故事板,在我执行转场的按钮上,我想要的是对我的文本字段进行验证,如果验证失败,我必须停止转场并发出警报。有什么办法呢?
回答by rob mayoff
If your deployment target is iOS 6.0 or later
如果您的部署目标是 iOS 6.0 或更高版本
You can simply implement the shouldPerformSegueWithIdentifier:sender:
method on your source view controller. Make this method return YES
if you want to perform the segue, or NO
if you don't.
您可以简单地shouldPerformSegueWithIdentifier:sender:
在源视图控制器上实现该方法。YES
如果您想执行转场,或者NO
如果您不想执行,请返回此方法。
If your deployment target is earlier than iOS 6.0
如果您的部署目标早于 iOS 6.0
You will need to change the way your segue is connected in the storyboard and write a little more code.
您需要更改 segue 在故事板中的连接方式并编写更多代码。
First, set up the segue from the button's view controller to the destination view controller, instead of directly from the button to the destination. Give the segue an identifier like ValidationSucceeded
.
首先,设置从按钮的视图控制器到目标视图控制器的转场,而不是直接从按钮到目标。给 segue 一个标识符,如ValidationSucceeded
.
Then, connect the button to an action on its view controller. In the action, perform the validation and either perform the segue or show an alert based on whether the validation succeeded. It will look something like this:
然后,将按钮连接到其视图控制器上的操作。在操作中,执行验证并根据验证是否成功执行 segue 或显示警报。它看起来像这样:
- (IBAction)performSegueIfValid:(id)sender {
if ([self validationIsSuccessful]) {
[self performSegueWithIdentifier:@"ValidationSucceeded" sender:self];
} else {
[self showAlertForValidationFailure];
}
}
回答by Shaun
What worked for me and what I believe to be the correct answer is to use UIViewController method found in Apple Developer Guide:
对我有用以及我认为正确的答案是使用Apple Developer Guide 中的UIViewController 方法:
shouldPerformSegueWithIdentifier:sender:
shouldPerformSegueWithIdentifier:sender:
I implemented my method like so:
我像这样实现了我的方法:
- (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;
}
Worked like a charm!
像魅力一样工作!
Updated with Swift for >= iOS 8:
使用 Swift 更新 >= 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
}
回答by Ali
I'll give you an example, here's my code:
我给你举个例子,这是我的代码:
- (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];
}
But it seems that's not working, in all cases my segue is performed. The answer is easy, we have to wire the segue from the view controller it self not from the Button.
但这似乎不起作用,在所有情况下都执行了我的转场。答案很简单,我们必须从视图控制器连接 segue,而不是从 Button 连接。