小编Der*_*Mei的帖子

仅在已填写所有文本字段时在Swift中启用按钮

我无法弄清楚如何更改我的代码,以便在填写完三个文本字段时启用导航栏中的"完成"按钮.

我目前有三个UITextFields和一个UIButtonItem.habitNameField和goalField都是手动文本字段,frequencyField是Picker View.

@IBOutlet weak var habitNameField: UITextField!
@IBOutlet weak var goalField: UITextField!
@IBOutlet weak var frequencyField: UITextField!

@IBOutlet weak var doneBarButton: UIBarButtonItem!
Run Code Online (Sandbox Code Playgroud)

我还有以下功能,当第一个字段中有类型的东西时,它可以工作.

func textField(habitNameField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let oldHabitNameText: NSString = habitNameField.text!
    let newHabitNameText: NSString = oldHabitNameText.stringByReplacingCharactersInRange(range, withString: string)
    doneBarButton.enabled = (newHabitNameText.length != 0)
    return true
}
Run Code Online (Sandbox Code Playgroud)

我尝试更改代码,以便将其他两个字段作为参数,并仅在填写完所有三个字段时启用doneBarButton.

func textField(habitNameField: UITextField, goalField: UITextField, frequencyField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let habitNameText: NSString = (habitNameField.text!).stringByReplacingCharactersInRange(range, withString: string) …
Run Code Online (Sandbox Code Playgroud)

iphone uitextfield ios swift swift2

15
推荐指数
2
解决办法
2万
查看次数

标签 统计

ios ×1

iphone ×1

swift ×1

swift2 ×1

uitextfield ×1