停止 segue 并显示警报

发布于 2025-01-07 21:48:35 字数 86 浏览 2 评论 0原文

使用 iOS 5 故事板,在按钮上执行 segue,我想要的是对我的文本字段进行验证,如果验证失败,我必须停止 segue 并发出警报。有什么方法可以做到吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

趁年轻赶紧闹 2025-01-14 21:48:35

如果您的部署目标是 iOS 6.0 或更高版本,

您只需在源视图控制器上实现 shouldPerformSegueWithIdentifier:sender: 方法即可。如果您想要执行转场,请让此方法返回 YES;如果您不想执行,请返回 NO

如果您的部署目标早于 iOS 6.0,

您将需要更改情节提要中的 Segue 连接方式并编写更多代码。

首先,设置从按钮的视图控制器到目标视图控制器的 Segue,而不是直接从按钮到目标。为 segue 提供一个标识符,例如 ValidationSucceeded

然后,将按钮连接到其视图控制器上的操作。在操作中,执行验证并根据验证是否成功执行 segue 或显示警报。它看起来像这样:

- (IBAction)performSegueIfValid:(id)sender {
    if ([self validationIsSuccessful]) {
        [self performSegueWithIdentifier:@"ValidationSucceeded" sender:self];
    } else {
        [self showAlertForValidationFailure];
    }
}

If your deployment target is iOS 6.0 or later

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.

If your deployment target is earlier than iOS 6.0

You will need to change the way your segue is connected in the storyboard and write a little more code.

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.

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:

- (IBAction)performSegueIfValid:(id)sender {
    if ([self validationIsSuccessful]) {
        [self performSegueWithIdentifier:@"ValidationSucceeded" sender:self];
    } else {
        [self showAlertForValidationFailure];
    }
}
故笙诉离歌 2025-01-14 21:48:35

对我有用并且我认为正确的答案是使用 Apple 开发人员指南

shouldPerformSegueWithIdentifier:sender:

我像这样实现了我的方法:

- (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;
}

工作得像个魅力!


针对 >= iOS 8 使用 Swift 进行了更新

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
}

What worked for me and what I believe to be the correct answer is to use UIViewController method found in Apple Developer Guide:

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:

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
}
简美 2025-01-14 21:48:35

我给你一个例子,这是我的代码:

- (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。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文