停止Segue并显示警报部分不起作用

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

我先前遇到了一个问题,即如果textFields为空,则停止segue并显示警报。这个问题解决了。我现在想通过一些逻辑测试来进一步运行这些文本字段的内容。但是,我的代码似乎没发现错误。代码如下:

I earlier had an issue with stopping the segue and showing an alert if the textFields were empty. That problem is solved. I now want to further run the content of these textfields through a few logical tests. However, my code doesn't seem to be catching the errors. Code as follows:

import Foundation import UIKit import Darwin class View3on3 : UIViewController, UITextFieldDelegate { @IBOutlet weak var APTeams: UITextField! @IBOutlet weak var APRounds: UITextField! @IBOutlet weak var APBreakers: UITextField! override func viewDidLoad() { super.viewDidLoad() initializeTextFields() } func initializeTextFields() { APTeams.delegate = self APTeams.keyboardType = UIKeyboardType.NumberPad APRounds.delegate = self APRounds.keyboardType = UIKeyboardType.NumberPad APBreakers.delegate = self APBreakers.keyboardType = UIKeyboardType.NumberPad } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if (APTeams.text!.isEmpty || APRounds.text!.isEmpty || APBreakers.text!.isEmpty) { let alertController: UIAlertController = UIAlertController( title: "Data missing!", message: "Please enter valid data into all 3 fields.", preferredStyle: UIAlertControllerStyle.Alert) let okAction = UIAlertAction( title: "OK", style: UIAlertActionStyle.Default, handler: nil) alertController.addAction(okAction) presentViewController(alertController, animated: true, completion: nil) } else if (Int(String(APTeams.text)) < Int(String(APBreakers.text))) { let alertController: UIAlertController = UIAlertController( title: "Math Error!", message: "The number of breaking teams cannot be more than the number of teams.", preferredStyle: UIAlertControllerStyle.Alert) let okAction = UIAlertAction( title: "OK", style: UIAlertActionStyle.Default, handler: nil) alertController.addAction(okAction) presentViewController(alertController, animated: true, completion: nil) } else if (Int(String(APTeams.text)) > 9999) { let alertController: UIAlertController = UIAlertController( title: "Math Error!", message: "Please input a number of teams less than 10,000.", preferredStyle: UIAlertControllerStyle.Alert) let okAction = UIAlertAction( title: "OK", style: UIAlertActionStyle.Default, handler: nil) alertController.addAction(okAction) presentViewController(alertController, animated: true, completion: nil) } else { let DestViewController : View3on3Results = segue.destinationViewController as! View3on3Results DestViewController.AP1 = APTeams.text! DestViewController.AP2 = APRounds.text! DestViewController.AP3 = APBreakers.text! //self.performSegueWithIdentifier("segueTest",sender: View3on3Results.self)// } } }

有人知道吗?非空字段的第一个测试工作正常,如果3个textField中的任何一个为空,则抛出错误。

Anyone know what's up? The first test for non-empty fields is working fine and throws an error if any of the 3 textFields are empty.

此外,如果有人知道如何测试以确保这些条目仅包含整数,不胜感激。那是我测试整数值之前需要进行的第二次测试。

Also, if anyone knows how to test to make sure the entries contain integers only, that'd be much appreciated. That's the second test I need to run before testing for the value of the integers.

附录:我正在使用XCode 7 beta 4,是用Swift 2.0编写的。

Addendum: I'm using XCode 7 beta 4, writing in Swift 2.0.

编辑:我的代码现在看起来像这样,现在segue发生了,根本没有检查。

My code now looks like this, and now the segue just happens without checking at all.

@derdida尝试按照您所说的做。这次袭击只是在没有检查任何条件的情况下发生的。

@derdida Tried to follow what you said. The segue just happened without checking any of the conditions. Could you correct my updated code?

func checkIfPassed() { if (APTeams.text!.isEmpty || APRounds.text!.isEmpty || APBreakers.text!.isEmpty) { let alertController: UIAlertController = UIAlertController( title: "Data missing!", message: "Please enter valid data into all 3 fields.", preferredStyle: UIAlertControllerStyle.Alert) let okAction = UIAlertAction( title: "OK", style: UIAlertActionStyle.Default, handler: nil) alertController.addAction(okAction) presentViewController(alertController, animated: true, completion: nil) } else if (Int(String(APTeams.text)) < Int(String(APBreakers.text))) { let alertController: UIAlertController = UIAlertController( title: "Math Error!", message: "The number of breaking teams cannot be more than the number of teams.", preferredStyle: UIAlertControllerStyle.Alert) let okAction = UIAlertAction( title: "OK", style: UIAlertActionStyle.Default, handler: nil) alertController.addAction(okAction) presentViewController(alertController, animated: true, completion: nil) } else if (Int(String(APTeams.text)) > 9999) { let alertController: UIAlertController = UIAlertController( title: "Math Error!", message: "Please input a number of teams less than 10,000.", preferredStyle: UIAlertControllerStyle.Alert) let okAction = UIAlertAction( title: "OK", style: UIAlertActionStyle.Default, handler: nil) alertController.addAction(okAction) presentViewController(alertController, animated: true, completion: nil) } else { // everything is fine, so manually go to your next ViewController self.performSegueWithIdentifier("3on3Segue", sender: nil) } } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { let DestViewController : View3on3Results = segue.destinationViewController as! View3on3Results DestViewController.AP1 = APTeams.text! DestViewController.AP2 = APRounds.text! DestViewController.AP3 = APBreakers.text! }

推荐答案

我会更改您的代码a一点。

I would change your code a little bit.

将ViewController1连接到ViewController2(而不是Button),因此您可以手动执行segue。我不会进入 prepareForSegue-我会编写自己的是否通过检查功能,就像这样:

Connect your ViewController1 to your ViewController2 (not the Button), so you are able to manually perform a segue. I would not go into "prepareForSegue" - i would write my own "check if passed" function, like that:

func checkIfPassed() { if (APTeams.text!.isEmpty || APRounds.text!.isEmpty || APBreakers.text!.isEmpty) { // show your first alert } else if(APTeams.text.toInt() != nil && ABreakers.text.toInt() != nil)) { // ok, both values are no integers, show your next error // show your second alert } else if(APTeams.text.toInt() < ABreakers.text.toInt()) { // values are integers, and ABreakers.text is > then APTeams.text // show your third alert, and so on } else { // everything is fine, so manually go to your next ViewController self.performSegueWithIdentifier("yourSegueIdentifier", nil) } }

更新:现在可以检查字符串是否为整数,并具有可选选项:(Swift 2.0)

Update: You can now check your Strings if they are integers with Optionals: (Swift 2.0)

let text:Int? = Int(textfield.text)

更多推荐

停止Segue并显示警报部分不起作用

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

发布评论

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

>www.elefans.com

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