使用文本字段委托来响应文本字段中的即时更改
use textfield delegate to respond to immediate change in textfield
我的 swift 代码现在使用文本字段委托来更改标签中显示的内容。问题是标签只有在用户输入数字然后删除时才会改变。正如您在下面的 gif 中看到的那样。我想要做的就是当用户输入 1 时,kim kardashian 出现在标签上。现在它做到了,但我必须输入 1,然后将其从文本字段中删除,然后 kim kardashian 才会出现在标签上。
import UIKit
import CoreData
class ViewController: UIViewController,UITextFieldDelegate {
@IBOutlet var labelName : UILabel!
@IBOutlet var enterT : UITextField!
lazy var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
override func viewDidLoad() {
super.viewDidLoad()
openDatabse()
enterT.delegate = self
}
func joke(at index : Int) {
let fetchRequest = NSFetchRequest<Users>(entityName: "Users")
fetchRequest.predicate = NSPredicate(format: "idx == %d", Int32(index))
do {
if let user = try context.fetch(fetchRequest).first {
labelName.text = user.username
}
} catch {
print("Could not fetch \(error) ")
}
}
func openDatabse()
{
let names = ["kim kardashian", "jessica biel", "Hailey Rienhart"]
for i in 0..<names.count {
let newUser = Users(context: context)
newUser.username = names[i]
newUser.idx = Int32(i + 1)
}
print("Storing Data..")
do {
try context.save()
} catch {
print("Storing data Failed", error)
}
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// return NO to not change text
print("While entering the characters this method gets called")
guard let index = Int(textField.text!) else {
// display an alert about invalid text
return true
}
joke(at: index )
return true
}}
如果您使用函数 touchesBegan()
并制作文本字段 .resignFirstResponder
,您还可以在其中添加一个更新函数。
你可以试试这个,
textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
和
func textFieldDidChange(_ textField: UITextField) {
}
textField.text
给出对 textField
= 进行任何更改之前的文本,即先前输入的 text
。您还需要向其中添加 replacementString
。在 textField
.
中输入的是 new text
所以,UITextFieldDelegate
方法 textField(_: shouldChangeCharactersIn: replacementString)
应该看起来像,
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// return NO to not change text
print("While entering the characters this method gets called")
guard let text = (textField.text as? NSString)?.replacingCharacters(in: range, with: string), let index = Int(text) else { //here....
// display an alert about invalid text
return true
}
joke(at: index )
return true
}
我的 swift 代码现在使用文本字段委托来更改标签中显示的内容。问题是标签只有在用户输入数字然后删除时才会改变。正如您在下面的 gif 中看到的那样。我想要做的就是当用户输入 1 时,kim kardashian 出现在标签上。现在它做到了,但我必须输入 1,然后将其从文本字段中删除,然后 kim kardashian 才会出现在标签上。
import UIKit
import CoreData
class ViewController: UIViewController,UITextFieldDelegate {
@IBOutlet var labelName : UILabel!
@IBOutlet var enterT : UITextField!
lazy var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
override func viewDidLoad() {
super.viewDidLoad()
openDatabse()
enterT.delegate = self
}
func joke(at index : Int) {
let fetchRequest = NSFetchRequest<Users>(entityName: "Users")
fetchRequest.predicate = NSPredicate(format: "idx == %d", Int32(index))
do {
if let user = try context.fetch(fetchRequest).first {
labelName.text = user.username
}
} catch {
print("Could not fetch \(error) ")
}
}
func openDatabse()
{
let names = ["kim kardashian", "jessica biel", "Hailey Rienhart"]
for i in 0..<names.count {
let newUser = Users(context: context)
newUser.username = names[i]
newUser.idx = Int32(i + 1)
}
print("Storing Data..")
do {
try context.save()
} catch {
print("Storing data Failed", error)
}
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// return NO to not change text
print("While entering the characters this method gets called")
guard let index = Int(textField.text!) else {
// display an alert about invalid text
return true
}
joke(at: index )
return true
}}
如果您使用函数 touchesBegan()
并制作文本字段 .resignFirstResponder
,您还可以在其中添加一个更新函数。
你可以试试这个,
textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
和
func textFieldDidChange(_ textField: UITextField) {
}
textField.text
给出对 textField
= 进行任何更改之前的文本,即先前输入的 text
。您还需要向其中添加 replacementString
。在 textField
.
text
所以,UITextFieldDelegate
方法 textField(_: shouldChangeCharactersIn: replacementString)
应该看起来像,
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// return NO to not change text
print("While entering the characters this method gets called")
guard let text = (textField.text as? NSString)?.replacingCharacters(in: range, with: string), let index = Int(text) else { //here....
// display an alert about invalid text
return true
}
joke(at: index )
return true
}