ios - unwind segue 在文本框值等于 nil 时取消

标签 ios swift textbox segue unwind-segue

如果文本框值等于 nil,我想取消展开转场。 下面是当前代码。我使用 Storyboard连接保存按钮(将文本值保存到数据库中)。我正在考虑使用 poptorootviewcontroller 来确保教科书在执行 segue 之前具有值。他们有什么办法可以用当前代码做到这一点吗?

在 mainView 中编写代码以展开回到-

@IBAction func unwind(_ segue: UIStoryboardSegue){
    print("Back in TableView")

}

childView 上的按钮执行 unwind segue-

 @IBAction func saveAddress(_ sender: UIButton) {
    // save values to dictionary save dictionary to firebase under user, uid, addresses
    // uialert controller if fields are not completed do not allow segue if fields are not complete
    // perform segue to addresstableview

    let addy1: String = address1Txt.text!
    let addy2: String = address2Txt.text!
    let aptNum: String = aptTxt.text!
    let city: String = cityTxt.text!
    let state: String = stateTxt.text!
    let zip: String = zipTxt.text!

    // add UIAlert controller for address field == nothing
    if addy1.isEmpty && aptNum.isEmpty && city.isEmpty && state.isEmpty && zip.isEmpty
    {
        //add UIAlert Controller DO NOT PERFORM SEGUE IF TRUE
    }

    Address.sharedInsance.typedNewAddress = addy1 + "," + addy2 + "," + aptNum + "," + city + "," + state + "," + zip

    print("address save print",addy1, addy2, aptNum, city, state, zip)
    let key = ref.child("address").childByAutoId().key
    let setter = false
    let addyDict = ["address line 1":addy1,"address line 2":addy2,"apt Number":aptNum,"city":city,"state":state,"zip":zip,"keyID": key, "setter": setter] as [String : Any]
    let userID = Auth.auth().currentUser?.uid
    let childUpdates = ["/address/\(key)": addyDict]
    ref.child("users").child(userID!).updateChildValues(childUpdates)

}

最佳答案

鉴于我没有看到你在你的 @IBAction 中执行 segue,我们必须假设你将你的按钮同时连接到 @IBAction和一个放松的segue。您应该从右侧面板最后一个选项卡上按钮的“Connections Inspector”中删除 segue,而是创建一个从 View Controller 本身到 exit outlet 的展开 segue:

create unwind segue

然后您可以在左侧面板中选择该转场,单击该转场的“属性检查器”,为其指定 Storyboard标识符:

enter image description here

然后您可以使用该 Storyboard标识符以编程方式执行 segue,仅当在您剩余的 @IBAction 中合适时,例如

@IBAction func didTapDoneButton(_ sender: Any) {
    // if the fields are `nil` or have a length of zero characters, show warning
    // and just `return` without ever performing unwind segue

    guard let firstName = textFieldFirstName.text?.trimmingCharacters(in: .whitespacesAndNewlines),
        let lastName = textFieldLastName.text?.trimmingCharacters(in: .whitespacesAndNewlines),
        firstName.count > 0, lastName.count > 0 else {
            let alert = UIAlertController(title: nil, message: "Please fill in all fields", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default))
            present(alert, animated: true)
            return
    }

    // do something with firstName and lastName

    save(firstName: firstName, lastName: lastName)

    // now perform unwind segue

    performSegue(withIdentifier: "unwindHome", sender: sender)
}

已经向您展示了这一点,我相信更可靠的方法是在输入必填字段之前不让用户点击“完成”/“保存”按钮。底线是,与其处理错误情况,不如设计一个不可能出现此类错误的 UI。

因此,其中的一些关键方面包括:

  • 调暗/禁用“完成”/“保存”按钮;
  • 为文本字段设置“占位符文本”以指示该字段是必需的。例如。 “名字(必填)”的占位符,以便用户知道它是必需的。
  • 为各种文本字段添加 editingChanged 方法,根据需要启用/禁用“完成”按钮:

    @IBAction func editingChanged(_ sender: UITextField) {
        if let firstName = textFieldFirstName.text?.trimmingCharacters(in: .whitespacesAndNewlines),
            let lastName = textFieldLastName.text?.trimmingCharacters(in: .whitespacesAndNewlines),
            firstName.count > 0, lastName.count > 0 {
            doneButton.isEnabled = true
        } else {
            doneButton.isEnabled = false
        }
    }
    

    显然将其与所有相关文本字段的“editingChanged”操作联系起来。

这会产生一个“完成”按钮(位于该演示右上角的按钮),该按钮在所有文本字段中至少有一些文本之前处于禁用状态:

enter image description here

最重要的是,首先要防止错误,而不是处理错误。

关于ios - unwind segue 在文本框值等于 nil 时取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49202642/

相关文章:

c# - 在 WPF 文本框中显示 "current working directory"

c# - 如何使多行文本框不接受 Enter 和 Backspace

ios - Swift 2 无代表

ios - 如何删除像橡皮擦一样的 CAShapeLayer? (不使用另一层白色) - 使用贝塞尔路径添加层

ios - 清除 AFNetworking 2 中响应的 cookie

ios - 迁移到惯用的 Swift 2 的 list (也就是 Swift 2 转换指南在哪里)?

ios - 如何将 NSString 传递给调用 printf 的函数?

ios - 使用 Swift Generics 识别子类适用于自定义类,但不适用于 UITapGestureRecognizer

从完成处理程序调用时,XCode 6 UILabel 不刷新文本

c# - 为什么在 C# 中启用多行的 TextBox 控件上禁用 CTRL+A 快捷方式?