如果文本字段中没有任何内容,我将尝试禁用继续按钮。 这是我的代码...
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var nounTextField: UITextField!
@IBOutlet weak var `continue`: UIButton!
var noun = String()
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func continueButton(sender: AnyObject) {
noun = nounTextField.text!
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let nvc = segue.destinationViewController as! ViewController2
nvc.noun2 = noun
}
}
最佳答案
因为你已经让你的类成为一个 UITextFieldDelegate 广告
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let text = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
if !text.isEmpty{
continueButton.userInteractionEnabled = true
} else {
continueButton.userInteractionEnabled = false
}
return true
}
同时更新你的 viewDidLoad 函数
override func viewDidLoad() {
super.viewDidLoad()
nounTextField.delegate = self
if nounTextField.text.isEmpty{
continueButton.userInteractionEnabled = false
}
}
关于ios - 如果文本字段为空,如何禁用按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34206648/