Ios UITextField更改时如何检查?
我正在尝试检查文本字段何时更改,相当于用于textView的函数-Ios UITextField更改时如何检查?,ios,swift,uitextfield,Ios,Swift,Uitextfield,我正在尝试检查文本字段何时更改,相当于用于textView的函数-textViewDidChange到目前为止,我已经完成了以下操作: func textFieldDidBeginEditing(textField: UITextField) { if self.status.text == "" && self.username.text == "" { self.topRightButton.enabled = false
textViewDidChange
到目前为止,我已经完成了以下操作:
func textFieldDidBeginEditing(textField: UITextField) {
if self.status.text == "" && self.username.text == "" {
self.topRightButton.enabled = false
} else {
self.topRightButton.enabled = true
}
}
哪种方式有效,但按下文本字段后,topRightButton
将立即启用,我希望只有在实际键入文本时才启用它?SWIFT
Swift 4.2
textfield.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged)
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
及
SWIFT 3和SWIFT 4.1
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged)
及
SWIFT 2.2
textfield.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged)
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
及
目标-C
[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
而textFieldDidChange方法是
-(void)textFieldDidChange :(UITextField *) textField{
//your code
}
SWIFT
Swift 4.2
textfield.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged)
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
及
SWIFT 3和SWIFT 4.1
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged)
及
SWIFT 2.2
textfield.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged)
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
及
目标-C
[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
而textFieldDidChange方法是
-(void)textFieldDidChange :(UITextField *) textField{
//your code
}
可以从UITextFieldDelegate使用此委托方法。它会随着角色的每一次变化而激发
(Objective C) textField:shouldChangeCharactersInRange:replacementString:
(Swift) textField(_:shouldChangeCharactersInRange:replacementString:)
但是,这仅在进行更改之前激发(实际上,只有在此处返回true时才会进行更改)。您可以从UITextFieldDelegate使用此委托方法。它会随着角色的每一次变化而激发
(Objective C) textField:shouldChangeCharactersInRange:replacementString:
(Swift) textField(_:shouldChangeCharactersInRange:replacementString:)
但是,这只会在做出更改之前触发(实际上,只有从这里返回true时才会进行更改)。到目前为止我处理它的方式:在
UITextFieldDelegate
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
// text hasn't changed yet, you have to compute the text AFTER the edit yourself
let updatedString = (textField.text as NSString?)?.stringByReplacingCharactersInRange(range, withString: string)
// do whatever you need with this updated string (your code)
// always return true so that changes propagate
return true
}
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
textField.addTarget(self, action: #selector(UITextFieldDelegate.textFieldShouldEndEditing(_:)), for: UIControlEvents.editingChanged)
}
迅捷4版
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let updatedString = (textField.text as NSString?)?.replacingCharacters(in: range, with: string)
return true
}
到目前为止我的处理方式是:在
UITextFieldDelegate
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
// text hasn't changed yet, you have to compute the text AFTER the edit yourself
let updatedString = (textField.text as NSString?)?.stringByReplacingCharactersInRange(range, withString: string)
// do whatever you need with this updated string (your code)
// always return true so that changes propagate
return true
}
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
textField.addTarget(self, action: #selector(UITextFieldDelegate.textFieldShouldEndEditing(_:)), for: UIControlEvents.editingChanged)
}
迅捷4版
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let updatedString = (textField.text as NSString?)?.replacingCharacters(in: range, with: string)
return true
}
您可以在interface builder中建立此连接
您可以在interface builder中建立此连接
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(sender:)), for: UIControlEvents.editingChanged)
斯威夫特3
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(sender:)), for: UIControlEvents.editingChanged)
这就是如何使用Swift 3添加
textField文本更改侦听器的方法:
将类声明为UITextFieldDelegate
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
// text hasn't changed yet, you have to compute the text AFTER the edit yourself
let updatedString = (textField.text as NSString?)?.stringByReplacingCharactersInRange(range, withString: string)
// do whatever you need with this updated string (your code)
// always return true so that changes propagate
return true
}
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
textField.addTarget(self, action: #selector(UITextFieldDelegate.textFieldShouldEndEditing(_:)), for: UIControlEvents.editingChanged)
}
然后只需添加textFieldShouldEndEditing函数:
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool { // do stuff
return true
}
@objc func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}
这就是如何使用Swift 3添加textField文本更改侦听器的方法:
将类声明为UITextFieldDelegate
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
// text hasn't changed yet, you have to compute the text AFTER the edit yourself
let updatedString = (textField.text as NSString?)?.stringByReplacingCharactersInRange(range, withString: string)
// do whatever you need with this updated string (your code)
// always return true so that changes propagate
return true
}
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
textField.addTarget(self, action: #selector(UITextFieldDelegate.textFieldShouldEndEditing(_:)), for: UIControlEvents.editingChanged)
}
然后只需添加textFieldShouldEndEditing函数:
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool { // do stuff
return true
}
@objc func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}
在Xcode 8,Swift 3中为我工作,如果你想检查每一次按键
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// Whatever code you want to run here.
// Keep in mind that the textfield hasn't yet been updated,
// so use 'string' instead of 'textField.text' if you want to
// access the string the textfield will have after a user presses a key
var statusText = self.status.text
var usernameText = self.username.text
switch textField{
case self.status:
statusText = string
case self.username:
usernameText = string
default:
break
}
if statusText == "" && usernameText == "" {
self.topRightButton.enabled = false
} else {
self.topRightButton.enabled = true
}
//Return false if you don't want the textfield to be updated
return true
}
在Xcode 8,Swift 3中为我工作,如果你想检查每一次按键
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// Whatever code you want to run here.
// Keep in mind that the textfield hasn't yet been updated,
// so use 'string' instead of 'textField.text' if you want to
// access the string the textfield will have after a user presses a key
var statusText = self.status.text
var usernameText = self.username.text
switch textField{
case self.status:
statusText = string
case self.username:
usernameText = string
default:
break
}
if statusText == "" && usernameText == "" {
self.topRightButton.enabled = false
} else {
self.topRightButton.enabled = true
}
//Return false if you don't want the textfield to be updated
return true
}
Swift 5.0
处理方法:
@objc func textFieldDidChange(_ textField: UITextField) {
}
@objc func textFieldDidChange(_ textField: UITextField) {
}
func textFieldDidChange(textField: UITextField) {
}
Swift 4.0
处理方法:
@objc func textFieldDidChange(_ textField: UITextField) {
}
@objc func textFieldDidChange(_ textField: UITextField) {
}
func textFieldDidChange(textField: UITextField) {
}
Swift 3.0
处理方法:
@objc func textFieldDidChange(_ textField: UITextField) {
}
@objc func textFieldDidChange(_ textField: UITextField) {
}
func textFieldDidChange(textField: UITextField) {
}
Swift 5.0
处理方法:
@objc func textFieldDidChange(_ textField: UITextField) {
}
@objc func textFieldDidChange(_ textField: UITextField) {
}
func textFieldDidChange(textField: UITextField) {
}
Swift 4.0
处理方法:
@objc func textFieldDidChange(_ textField: UITextField) {
}
@objc func textFieldDidChange(_ textField: UITextField) {
}
func textFieldDidChange(textField: UITextField) {
}
Swift 3.0
处理方法:
@objc func textFieldDidChange(_ textField: UITextField) {
}
@objc func textFieldDidChange(_ textField: UITextField) {
}
func textFieldDidChange(textField: UITextField) {
}
Swift 3.0.1+(其他一些Swift 3.0答案不是最新的)
Swift 3.0.1+(其他一些Swift 3.0答案不是最新的)
可能使用RxSwift?
需要
明显增加进口
import RxSwift
import RxCocoa
所以你有一个textfield:UITextField
let observable: Observable<String?> = textField.rx.text.asObservable()
observable.subscribe(
onNext: {(string: String?) in
print(string!)
})
let observable:observable=textField.rx.text.asObservable()
可观察的(
onNext:{(字符串:字符串?)在
打印(字符串!)
})
你还有其他3种方法
一个错误
未完成
onDisposed
onNext
可能使用RxSwift?
需要
明显增加进口
import RxSwift
import RxCocoa
所以你有一个textfield:UITextField
let observable: Observable<String?> = textField.rx.text.asObservable()
observable.subscribe(
onNext: {(string: String?) in
print(string!)
})
let observable:observable=textField.rx.text.asObservable()
可观察的(
onNext:{(字符串:字符串?)在
打印(字符串!)
})
你还有其他3种方法
一个错误
未完成
onDisposed
onNext
swift 4
textField.addTarget(self, action: #selector(textIsChanging), for: UIControlEvents.editingChanged)
@objc func textIsChanging(_ textField:UITextField) {
print ("TextField is changing")
}
在viewDidLoad()中:
添加此功能:
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool { // do stuff
return true
}
@objc func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}
swift 4
textField.addTarget(self, action: #selector(textIsChanging), for: UIControlEvents.editingChanged)
@objc func textIsChanging(_ textField:UITextField) {
print ("TextField is changing")
}
在viewDidLoad()中:
添加此功能:
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool { // do stuff
return true
}
@objc func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}
Swift 4
textField.addTarget(self, action: #selector(textIsChanging), for: UIControlEvents.editingChanged)
@objc func textIsChanging(_ textField:UITextField) {
print ("TextField is changing")
}
符合UITextFieldDelegate的要求
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// figure out what the new string will be after the pending edit
let updatedString = (textField.text as NSString?)?.replacingCharacters(in: range, with: string)
// Do whatever you want here
// Return true so that the change happens
return true
}
Swift 4
textField.addTarget(self, action: #selector(textIsChanging), for: UIControlEvents.editingChanged)
@objc func textIsChanging(_ textField:UITextField) {
print ("TextField is changing")
}
符合UITextFieldDelegate的要求
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// figure out what the new string will be after the pending edit
let updatedString = (textField.text as NSString?)?.replacingCharacters(in: range, with: string)
// Do whatever you want here
// Return true so that the change happens
return true
}
Swift 4
textField.addTarget(self, action: #selector(textIsChanging), for: UIControlEvents.editingChanged)
@objc func textIsChanging(_ textField:UITextField) {
print ("TextField is changing")
}
如果您希望在用户完全输入后进行更改(一旦用户关闭键盘或按enter键,将调用该更改)
Swift 4
textField.addTarget(self, action: #selector(textIsChanging), for: UIControlEvents.editingChanged)
@objc func textIsChanging(_ textField:UITextField) {
print ("TextField is changing")
}
如果您希望在用户完全输入后进行更改(一旦用户关闭键盘或按enter键,将调用该更改)
您应该遵循以下步骤:
对文本字段进行出口引用
将AssignUITextFieldDelegate分配给控制器类
配置yourTextField.delegate
实现您需要的任何功能
示例代码:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var yourTextFiled : UITextField!
override func viewDidLoad() {
super.viewDidLoad()
yourTextFiled.delegate = self
}
func textFieldDidEndEditing(_ textField: UITextField) {
// your code
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// your code
}
.
.
.
}
您应该遵循以下步骤:
对文本字段进行出口引用
将AssignUITextFieldDelegate分配给控制器类
配置yourTextField.delegate
实现您需要的任何功能
示例代码:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var yourTextFiled : UITextField!
override func viewDidLoad() {
super.viewDidLoad()
yourTextFiled.delegate = self
}
func textFieldDidEndEditing(_ textField: UITextField) {
// your code
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// your code
}
.
.
.
}
Swift 4.2
textfield.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged)
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
在viewDidLoad中编写此代码
// to detect if TextField changed
TextField.addTarget(self, action: #selector(textFieldDidChange(_:)),
for: UIControl.Event.editingChanged)
@objc func textFieldDidChange(_ textField: UITextField) {
// do something
}
在viewDidLoad外部编写此文件
// to detect if TextField changed
TextField.addTarget(self, action: #selector(textFieldDidChange(_:)),
for: UIControl.Event.editingChanged)
@objc func textFieldDidChange(_ textField: UITextField) {
// do something
}
您可以通过UIControl.event.editingdidbeagin或任何您想要检测的内容来更改事件。Swift 4.2
textfield.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged)
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
在viewDidLoad中编写此代码
// to detect if TextField changed
TextField.addTarget(self, action: #selector(textFieldDidChange(_:)),
for: UIControl.Event.editingChanged)
@objc func textFieldDidChange(_ textField: UITextField) {
// do something
}
在viewDidLoad外部编写此文件
// to detect if TextField changed
TextField.addTarget(self, action: #selector(textFieldDidChange(_:)),
for: UIControl.Event.editingChanged)
@objc func textFieldDidChange(_ textField: UITextField) {
// do something
}
你可以改变主意