这是我在 UITextfield 的子类中所做的,
override public var text:String? {
didSet {
NSLog("text = \(text)")
}
willSet {
NSLog("will change from \(text) to \(newValue)")
}}
现在,当我使用 myTextField.text 将值设置为变量时,它显示 Bad Access。当我记录文本时,它显示一些垃圾值作为文本。
最佳答案
看看这个 UITextField 的子类。
// CustomTextField.swift
import UIKit
class CustomTextField: UITextField {
override internal var text : String? {
didSet {
if !(text != nil && text!.isEmpty) {
print("text = \(text)")
}
}
willSet {
if !(text != nil && text!.isEmpty) {
print("will change from \(text) to \(newValue)")
}
}
}
}
我检查了 View Controller 。有效:
override func viewDidLoad() {
super.viewDidLoad()
sampleTextField.text = "sample Text -1"
}
override func viewDidAppear(animated: Bool) {
sampleTextField.text = "sample Text - 2"
}
关于ios - UITextfield 对覆盖文本属性的错误访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37657071/