停止 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的部署目标是 iOS 6.0 或更高版本,
您只需在源视图控制器上实现
shouldPerformSegueWithIdentifier:sender:
方法即可。如果您想要执行转场,请让此方法返回YES
;如果您不想执行,请返回NO
。如果您的部署目标早于 iOS 6.0,
您将需要更改情节提要中的 Segue 连接方式并编写更多代码。
首先,设置从按钮的视图控制器到目标视图控制器的 Segue,而不是直接从按钮到目标。为 segue 提供一个标识符,例如
ValidationSucceeded
。然后,将按钮连接到其视图控制器上的操作。在操作中,执行验证并根据验证是否成功执行 segue 或显示警报。它看起来像这样:
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 returnYES
if you want to perform the segue, orNO
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:
对我有用并且我认为正确的答案是使用 Apple 开发人员指南:
shouldPerformSegueWithIdentifier:sender:
我像这样实现了我的方法:
工作得像个魅力!
针对 >= iOS 8 使用 Swift 进行了更新:
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:
Worked like a charm!
Updated with Swift for >= iOS 8:
我给你一个例子,这是我的代码:
但似乎这不起作用,在所有情况下我的转场都被执行。
答案很简单,我们必须从视图控制器本身而不是按钮连接 Segue。
I'll give you an example, here's my code:
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.