【问题标题】:Stop Segue and show alert partially not working - Xcode停止 Segue 并显示警报部分不起作用 - Xcode
【发布时间】: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)) &lt; Int(String(APBreakers.text)))else if APTeams.text.toInt() &lt; APBreakers.text.toInt() else if (Int(String(APTeams.text)) &gt; 9999)else if APTeams.text.toInt() &gt; 9999
  • 我在 Swift 2 中使用 Xcode 7 beta 4。toInt 初始化程序已被删除 @DharmeshKheni :/
  • 好吧,我不知道,因为我使用的是 6.4。

标签: xcode swift segue alert


【解决方案1】:

我会稍微更改您的代码。

将您的 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)
        }


    }

更新:您现在可以使用 Optionals 检查您的字符串是否为整数:(Swift 2.0)

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

【讨论】:

  • 我是 Swift 编码的新手,我了解逻辑测试,但绝对不知道如何调用 Segue。如果您查看我的 Segue 代码,我也使用 segue 将数据传递给下一个 View Controller。你能写出代码来告诉我这是如何完成的吗?真的很抱歉,提前谢谢!
  • 您仍然可以使用“prepareForSegue” - 使用 performSegueWithIdentifier 就像您将 UIButton 与 ViewController 连接一样,然后按下此按钮。您只是手动执行此操作。
  • 如果您在“prepareForSegue”函数“外部”编写该代码,它会更清晰 - 您可以“重用”您的代码,也许可以用于另一个控制器。当所有值都被验证时,你就会进入 segues。
  • 好的,我想我明白了。干净多了。两个简单的问题:1)为什么这段代码不起作用? 2) 我怎么知道我的 SegueIdentifier 是什么?
  • 哪个错误?单击您的 Segue(在您的 Storyboard 中) - 在右侧您可以编写标识符:或在此处查看:i.stack.imgur.com/3w9Lj.png
【解决方案2】:

Prepare for segue 可让您根据需要配置数据并触发执行 segue。你应该做什么:获取将触发此 segue 的操作:点击手势、按钮操作或其他任何操作。在那里,您进行验证,如果一切正常,请执行

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

【讨论】:

    猜你喜欢
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多