簡體   English   中英

停止Segue並顯示警報部分不起作用-Xcode

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

之前,我有一個問題,即停止停頓並在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個textField中的任何一個為空,則拋出錯誤。

另外,如果有人知道如何測試以確保條目僅包含整數,那么將不勝感激。 那是在測試整數值之前我需要運行的第二個測試。

附錄:我正在使用XCode 7 beta 4,以Swift 2.0編寫。

編輯:我的代碼現在看起來像這樣,現在segue發生了,根本沒有檢查。

@derdida嘗試按照您說的去做。 這次襲擊只是在沒有檢查任何條件的情況下發生的。 您可以更正我的更新代碼嗎?

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)   

准備進行安全保護將使您可以根據需要配置數據,並執行安全保護。 您應該執行的操作:采取觸發操作的動作:輕按手勢,按鈕動作或其他任何動作。 在那里,您進行驗證,如果一切正常,請執行

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM