如果文本框值等于nil,我想取消展开segue。 下面是当前的代码。我使用故事板连接保存按钮(将文本值保存到数据库中)。我正在考虑使用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)
}
答案 0 :(得分:1)
鉴于我没有看到您在@IBAction
中执行segue,我们必须假设您将按钮连接到@IBAction
和展开segue。您应该从右侧面板上最后一个选项卡上的按钮“Connections Inspector”中删除segue,而是从视图控制器本身创建一个展开segue到出口:
然后,您可以在左侧面板中选择该segue,单击该segue的“属性检查器”,为其指定一个故事板标识符:
然后,您可以使用该故事板标识符以编程方式执行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”操作联系起来。
这会产生一个“完成”按钮(本演示文稿右上角的按钮),在所有文本字段中至少有一些文本被禁用:
底线,而不是处理错误,首先阻止它。
答案 1 :(得分:0)
您应该覆盖shouldPerformSegue方法,如果教科书字段为空,则返回false。
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
}