ios - UITextfield 对覆盖文本属性的错误访问

标签 ios iphone swift uitextfield textfield

这是我在 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/

相关文章:

ios - 加载保存到文档目录的图像只能工作一次,然后再也不会加载

cocoa - iPad 键盘编程 - 几个问题

iphone - 在 iOS 上的 UIWebView 中观看 Vimeo 视频

ios - 关于自定义全局UIButton的问题

ios - 仅当发生碰撞时才运行 skaction

ios - 我无法将 UIButton 的 IBAction 连接到 ViewController 类

ios - 如何卡住文本域的编辑?

ios - 在 CATextLayer 中格式化不同的特定字母?

ios - 为什么 UITableView 总是可以滚动而 UIScrollView 不能

iphone - 如何在终止应用程序之前保留 iPhone 应用程序状态?