简体   繁体   English

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

[英]Stop Segue and show alert partially not working - Xcode

I earlier had an issue with stopping the segue and showing an alert if the textFields were empty. 之前,我有一个问题,即停止停顿并在textFields为空时显示警报。 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)//
        }



        }

}

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. 非空字段的第一个测试工作正常,如果3个textField中的任何一个为空,则抛出错误。

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. 那是在测试整数值之前我需要运行的第二个测试。

Addendum: I'm using XCode 7 beta 4, writing in Swift 2.0. 附录:我正在使用XCode 7 beta 4,以Swift 2.0编写。

Edit: My code now looks like this, and now the segue just happens without checking at all. 编辑:我的代码现在看起来像这样,现在segue发生了,根本没有检查。

@derdida Tried to follow what you said. @derdida尝试按照您说的去做。 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!
        }

I would change your code a little bit. 我会稍微更改您的代码。

Connect your ViewController1 to your ViewController2 (not the Button), so you are able to manually perform a segue. 将ViewController1连接到ViewController2(而不是Button),因此您可以手动执行segue。 I would not go into "prepareForSegue" - i would write my own "check if passed" function, like that: 我不会进入“ 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)
        }


    }

Update: You can now check your Strings if they are integers with Optionals: (Swift 2.0) 更新:您现在可以使用可选选项检查字符串是否为整数:(Swift 2.0)

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

Prepare for segue will let you configure your data as needed and will fire perform segue. 准备进行安全保护将使您可以根据需要配置数据,并执行安全保护。 What you should do: get action that will trigger this segue: Tap gesture, button action or anything else. 您应该执行的操作:采取触发操作的动作:轻按手势,按钮动作或其他任何动作。 There, you do the validation and if everything is ok, do 在那里,您进行验证,如果一切正常,请执行

self.performSegueWithIdentifier("segueTest",sender: View3on3Results.self)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM