【发布时间】:2023-03-15 22:50:01
【问题描述】:
我之前遇到过停止 segue 并在 textFields 为空时显示警报的问题。那个问题就解决了。我现在想通过一些逻辑测试进一步运行这些文本字段的内容。但是,我的代码似乎没有发现错误。代码如下:
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 个文本字段中的任何一个为空,则会引发错误。
另外,如果有人知道如何测试以确保条目仅包含整数,那将不胜感激。这是在测试整数值之前我需要运行的第二个测试。
附录:我使用的是 XCode 7 beta 4,使用 Swift 2.0 编写。
编辑:我的代码现在看起来像这样,现在转场只是发生了,根本没有检查。
@derdida 试图按照你说的去做。 segue 只是在没有检查任何条件的情况下发生。你能更正我更新的代码吗?
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!
}
【问题讨论】:
-
更改此行:
else if (Int(String(APTeams.text)) < Int(String(APBreakers.text)))与else if APTeams.text.toInt() < APBreakers.text.toInt()和else if (Int(String(APTeams.text)) > 9999)与else if APTeams.text.toInt() > 9999 -
我在 Swift 2 中使用 Xcode 7 beta 4。toInt 初始化程序已被删除 @DharmeshKheni :/
-
好吧,我不知道,因为我使用的是 6.4。