停止segue并显示警报

编程入门 行业动态 更新时间:2024-10-19 15:36:00
本文介绍了停止segue并显示警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用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?

推荐答案

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

您可以在源视图控制器上简单地实现 shouldPerformSegueWithIdentifier:sender:方法。如果你想执行segue,请使此方法返回 YES ,否则返回 NO 。

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.

您需要在故事板中更改segue的连接方式并编写更多代码。

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

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

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或根据验证是否成功显示警报。它看起来像这样:

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

更多推荐

停止segue并显示警报

本文发布于:2023-11-27 03:58:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636486.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   segue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!