我已经检查过一些帖子,但无法找到有关它的清晰解释。如何以编程方式创建UITextField
事件?
答案 0 :(得分:8)
开始活动
yourTextField.addTarget(self, action: "myTargetEditingDidBeginFunction:", forControlEvents: UIControlEvents.EditingDidBegin)
func myTargetEditingDidBeginFunction(textField: UITextField) {
// do your stuff
}
删除活动
yourTextField.addTarget(self, action: "myTargetEditingDidEndFunction:", forControlEvents: UIControlEvents.EditingDidEnd)
func myTargetEditingDidEndFunction(textField: UITextField) {
// user touch field
}
其他一些委托方法
protocol UITextFieldDelegate : NSObjectProtocol {
optional func textFieldShouldBeginEditing(textField: UITextField) -> Bool // return NO to disallow editing.
optional func textFieldDidBeginEditing(textField: UITextField) // became first responder
optional func textFieldShouldEndEditing(textField: UITextField) -> Bool // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
optional func textFieldDidEndEditing(textField: UITextField) // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
optional func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool // return NO to not change text
optional func textFieldShouldClear(textField: UITextField) -> Bool // called when clear button pressed. return NO to ignore (no notifications)
optional func textFieldShouldReturn(textField: UITextField) -> Bool // called when 'return' key pressed. return NO to ignore.
}
来自swift docs的:
struct UIControlEvents : RawOptionSetType {
init(_ rawValue: UInt)
init(rawValue: UInt)
static var TouchDown: UIControlEvents { get } // on all touch downs
static var TouchDownRepeat: UIControlEvents { get } // on multiple touchdowns (tap count > 1)
static var TouchDragInside: UIControlEvents { get }
static var TouchDragOutside: UIControlEvents { get }
static var TouchDragEnter: UIControlEvents { get }
static var TouchDragExit: UIControlEvents { get }
static var TouchUpInside: UIControlEvents { get }
static var TouchUpOutside: UIControlEvents { get }
static var TouchCancel: UIControlEvents { get }
static var ValueChanged: UIControlEvents { get } // sliders, etc.
static var EditingDidBegin: UIControlEvents { get } // UITextField
static var EditingChanged: UIControlEvents { get }
static var EditingDidEnd: UIControlEvents { get }
static var EditingDidEndOnExit: UIControlEvents { get } // 'return key' ending editing
static var AllTouchEvents: UIControlEvents { get } // for touch events
static var AllEditingEvents: UIControlEvents { get } // for UITextField
static var ApplicationReserved: UIControlEvents { get } // range available for application use
static var SystemReserved: UIControlEvents { get } // range reserved for internal framework use
static var AllEvents: UIControlEvents { get }
}
答案 1 :(得分:0)
Swift 4.2:
此示例执行“ editingChanged”事件,但您可以对其进行修改以用于其他任何UITextField events。
MyTextField.addTarget(self, action: #selector(MyTextField_EditingChanged)
, for: UIControl.Event.editingChanged)
@objc func MyTextField_EditingChanged(textField: UITextField) {
print(textField.text)
}