Swift3 swift 3:单击后退导航按钮时如何绕过文本字段shouldendediting func

Swift3 swift 3:单击后退导航按钮时如何绕过文本字段shouldendediting func,swift3,xcode8,Swift3,Xcode8,我已经实现了textFieldShouldEndEditing函数,以在用户退出字段之前验证输入到每个字段中的数据。如果输入的数据未通过验证,则不允许用户退出该字段 然而,虽然这样做很好,但我遇到的问题是,当用户单击Back navigation按钮时,它会被触发。无论出于何种原因,当发生这种情况时,都会显示先前的视图控制器。如果随后返回视图控制器,则字段根本不可编辑。没有显示键盘,光标没有进入字段,什么也没有 我的问题是,当用户单击“后退”按钮时,如何绕过textFieldShouldEndE

我已经实现了textFieldShouldEndEditing函数,以在用户退出字段之前验证输入到每个字段中的数据。如果输入的数据未通过验证,则不允许用户退出该字段

然而,虽然这样做很好,但我遇到的问题是,当用户单击Back navigation按钮时,它会被触发。无论出于何种原因,当发生这种情况时,都会显示先前的视图控制器。如果随后返回视图控制器,则字段根本不可编辑。没有显示键盘,光标没有进入字段,什么也没有

我的问题是,当用户单击“后退”按钮时,如何绕过textFieldShouldEndEditing函数,或者如何在验证函数中检测到单击了“后退”按钮,以便返回true并解决此问题。任何帮助都将不胜感激

下面是我的textfieldshouldenediting func(名为return true或false的验证函数):


这是用swift 3和Xcode 8编写的。

好的,我想出来了。以下是我必须做的:

1-我创建了一个全局布尔布尔布尔布尔值boolCancelled并将其设置为false。用于标记是否单击了“取消”按钮

2-覆盖导航栏上的后退按钮。为此,我在viewDidLoad()方法中添加了以下代码:

3-我还创建了backButtonAction函数:

func backButtonAction(){
    // set the boolean to True to indicate the Cancel button was clicked
    boolCancelled = true
    self.navigationController?.popToRootViewController(animated: true)
}
internal func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
    if (boolCancelled) {
        return true
    } else if (tField.isEditing) {
        return tFieldValidated()
    } else if (oField.isEditing) {
        return oFieldValidated()
    } else {
        return distanceFieldValidated()
    }
}
4-在viewDidLoad()中将boolCancelled默认为false

5-在textFieldShouldEndEditing函数中添加了boolCancelled检查:

func backButtonAction(){
    // set the boolean to True to indicate the Cancel button was clicked
    boolCancelled = true
    self.navigationController?.popToRootViewController(animated: true)
}
internal func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
    if (boolCancelled) {
        return true
    } else if (tField.isEditing) {
        return tFieldValidated()
    } else if (oField.isEditing) {
        return oFieldValidated()
    } else {
        return distanceFieldValidated()
    }
}

现在,当我单击“取消”按钮时,文本字段shouldendediting将返回true,并且所有内容都按照我需要的方式工作。

好的,我找到了答案。以下是我必须做的:

1-我创建了一个全局布尔布尔布尔布尔值boolCancelled并将其设置为false。用于标记是否单击了“取消”按钮

2-覆盖导航栏上的后退按钮。为此,我在viewDidLoad()方法中添加了以下代码:

3-我还创建了backButtonAction函数:

func backButtonAction(){
    // set the boolean to True to indicate the Cancel button was clicked
    boolCancelled = true
    self.navigationController?.popToRootViewController(animated: true)
}
internal func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
    if (boolCancelled) {
        return true
    } else if (tField.isEditing) {
        return tFieldValidated()
    } else if (oField.isEditing) {
        return oFieldValidated()
    } else {
        return distanceFieldValidated()
    }
}
4-在viewDidLoad()中将boolCancelled默认为false

5-在textFieldShouldEndEditing函数中添加了boolCancelled检查:

func backButtonAction(){
    // set the boolean to True to indicate the Cancel button was clicked
    boolCancelled = true
    self.navigationController?.popToRootViewController(animated: true)
}
internal func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
    if (boolCancelled) {
        return true
    } else if (tField.isEditing) {
        return tFieldValidated()
    } else if (oField.isEditing) {
        return oFieldValidated()
    } else {
        return distanceFieldValidated()
    }
}

现在,当我单击“取消”按钮时,文本字段shouldendediting将返回true,并且所有内容都按照我需要的方式工作。

如果用户使用刷卡返回,该怎么办?如果用户使用刷卡返回,该怎么办?