gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:13:28 24 4
gpt4 key购买 nike

我之前遇到过停止转场并在文本字段为空时显示警报的问题。这个问题就解决了。我现在想通过一些逻辑测试进一步运行这些文本字段的内容。但是,我的代码似乎没有捕获错误。代码如下:

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!
}

最佳答案

我会稍微改变一下你的代码。

将 ViewController1 连接到 ViewController2(不是 Button),这样您就可以手动执行 Segue。我不会进入“prepareForSegue” - 我会编写自己的“检查是否通过”函数,如下所示:

    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)

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

关于xcode - 停止 Segue 并显示警报部分不起作用 - Xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31580960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com