您可以
UITextField在类中声明一个属性,然后在中为它分配当前文本字段
textFieldDidBeginEditing。然后,您可以在需要时随时调用此文本字段。
class ViewController : UIViewController, UITextFieldDelegate { var activeTextField = UITextField() // Assign the newly active text field to your activeTextField variable func textFieldDidBeginEditing(textField: UITextField) { self.activeTextField = textField } // Call activeTextField whenever you need to func anotherMethod() { // self.activeTextField.text is an optional, we safely unwrap it here if let activeTextFieldText = self.activeTextField.text { print("Active text field's text: (activeTextFieldText)") return; } print("Active text field is empty") }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)