为什么这个if参数没有实现?

时间:2015-07-23 12:00:16

标签: ios

我之前提出了一个问题(Stop Segue and show alert partially not working - Xcode

并收到答复,要求我在segue之前实施检查文本字段的不同方法。然而,我意识到有些事情是不对的,因为我的一个IF论点运行得非常好。

这是代码:

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 touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?){
    view.endEditing(true)
    super.touchesBegan(touches, withEvent: event)
}


    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: "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
        {
            let DestViewController : View3on3Results = segue.destinationViewController as! View3on3Results

            DestViewController.AP1 = APTeams.text!
            DestViewController.AP2 = APRounds.text!
            DestViewController.AP3 = APBreakers.text!

        } 
        }
}

它通过第一个IF函数运行文本字段输入完全正常。屏幕截图:

enter image description here enter image description here

但当我把它键入时(它应该满足第一个ELSE IF功能并显示警告:

enter image description here

它只是正常分裂。

为什么会这样?非常感谢。 =)

3 个答案:

答案 0 :(得分:0)

else if (Int(String(APTeams.text)) < Int(String(APBreakers.text)))

这不是执行因为它不是真的。将其拆分,在此检查之前首先将字符串转换为int,然后在此语句中直接比较ints。

答案 1 :(得分:0)

else if (Int(APTeams.text!) > Int(APBreakers.text!))

请检查我已编辑了我的答案

答案 2 :(得分:0)

感谢@RoryMcKinnel和@ Paulw11提供有关调试和检查变量的建议。一旦我这样做,我就能找到问题所在。这只是我初始化变量的时间和变异的时间问题。

此处修正了代码:

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 touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?){
    view.endEditing(true)
    super.touchesBegan(touches, withEvent: event)
}


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        var team:Int = 0
        var round:Int = 0
        var breaky:Int = 0

        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
        {
            var teams:String = String(APTeams.text!)
            var rounds:String = String(APRounds.text!)
            var breakers:String = String(APBreakers.text!)

            team = Int(teams)!
            round = Int(rounds)!
            breaky = Int(breakers)!
        }

        if ( breaky > team)
        {
            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 (team > 9999)
        {
            let alertController: UIAlertController = UIAlertController(
                title: "Too Many Teams!",
                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 if (round < 0 || round > 9)
        {
            let alertController: UIAlertController = UIAlertController(
                title: "Too Many Rounds!",
                message: "Please input a number of rounds between 1 and 9.",
                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!

        }



        }


}
相关问题