unwind segue 在文本框值等于 nil 时取消
unwind segue cancel upon textbox value equal to nil
如果文本框值等于 nil,我想取消展开转场。
下面是当前代码。我使用故事板连接保存按钮(将文本值保存到数据库中)。我正在考虑使用 poptorootviewcontroller 来确保教科书在执行 segue 之前具有值。他们有什么办法可以用当前代码做到这一点吗?
mainView 内的代码展开回-
@IBAction func unwind(_ segue: UIStoryboardSegue){
print("Back in TableView")
}
childView 上的按钮执行展开 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)
}
如果教科书字段为空,您应该覆盖 shouldPerformSegue 方法和 return false。
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
}
鉴于我没有看到您在 @IBAction
中执行 segue,我们必须假设您将按钮连接到 @IBAction
和 unwind segue。您应该从右侧面板最后一个选项卡上的按钮 "Connections Inspector" 中删除转场,而是创建一个从视图控制器本身到出口出口的展开转场:
然后您可以 select 左侧面板中的那个 segue,单击那个 segue 的 "attributes inspector",给它一个故事板标识符:
然后您可以使用该情节提要标识符以编程方式执行 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)
}
已经向您展示了这一点,我相信更可靠的方法是在输入必填字段之前不让用户点击 "done"/"save" 按钮。最重要的是,与其处理错误情况,不如设计一个 UI 不可能发生此类错误的地方。
因此,其中的一些关键方面包括:
- Dim/disable "done"/"save" 按钮;
- 为文本字段设置 "placeholder text" 以指示该字段是必需的。例如。 "First name (required)" 的占位符,以便用户知道它是必需的。
为 enables/disables "done" 按钮的各种文本字段添加 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" 操作挂钩。
这会产生一个 "Done" 按钮(位于该演示右上角的按钮),该按钮在所有文本字段中至少有一些文本之前处于禁用状态:
最重要的是,首先要防止错误,而不是处理错误。
如果文本框值等于 nil,我想取消展开转场。 下面是当前代码。我使用故事板连接保存按钮(将文本值保存到数据库中)。我正在考虑使用 poptorootviewcontroller 来确保教科书在执行 segue 之前具有值。他们有什么办法可以用当前代码做到这一点吗?
mainView 内的代码展开回-
@IBAction func unwind(_ segue: UIStoryboardSegue){
print("Back in TableView")
}
childView 上的按钮执行展开 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)
}
如果教科书字段为空,您应该覆盖 shouldPerformSegue 方法和 return false。
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
}
鉴于我没有看到您在 @IBAction
中执行 segue,我们必须假设您将按钮连接到 @IBAction
和 unwind segue。您应该从右侧面板最后一个选项卡上的按钮 "Connections Inspector" 中删除转场,而是创建一个从视图控制器本身到出口出口的展开转场:
然后您可以 select 左侧面板中的那个 segue,单击那个 segue 的 "attributes inspector",给它一个故事板标识符:
然后您可以使用该情节提要标识符以编程方式执行 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)
}
已经向您展示了这一点,我相信更可靠的方法是在输入必填字段之前不让用户点击 "done"/"save" 按钮。最重要的是,与其处理错误情况,不如设计一个 UI 不可能发生此类错误的地方。
因此,其中的一些关键方面包括:
- Dim/disable "done"/"save" 按钮;
- 为文本字段设置 "placeholder text" 以指示该字段是必需的。例如。 "First name (required)" 的占位符,以便用户知道它是必需的。
为 enables/disables "done" 按钮的各种文本字段添加
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" 操作挂钩。
这会产生一个 "Done" 按钮(位于该演示右上角的按钮),该按钮在所有文本字段中至少有一些文本之前处于禁用状态:
最重要的是,首先要防止错误,而不是处理错误。