【发布时间】:2015-02-07 01:44:51
【问题描述】:
我有一个非常简单的 UITextView 子类,它添加了“占位符”功能,您可以在文本字段对象中找到该功能。这是我的子类代码:
import UIKit
import Foundation
@IBDesignable class PlaceholderTextView: UITextView, UITextViewDelegate
{
@IBInspectable var placeholder: String = "" {
didSet {
setPlaceholderText()
}
}
private let placeholderColor: UIColor = UIColor.lightGrayColor()
private var textColorCache: UIColor!
override init(frame: CGRect) {
super.init(frame: frame)
self.delegate = self
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.delegate = self
}
func textViewDidBeginEditing(textView: UITextView) {
if textView.text == placeholder {
textView.text = ""
textView.textColor = textColorCache
}
}
func textViewDidEndEditing(textView: UITextView) {
if textView.text == "" && placeholder != "" {
setPlaceholderText()
}
}
func setPlaceholderText() {
if placeholder != "" {
if textColorCache == nil { textColorCache = self.textColor }
self.textColor = placeholderColor
self.text = placeholder
}
}
}
在身份检查器中将UITextView 对象的类更改为PlaceholderTextView 后,我可以在属性检查器中设置Placeholder 属性就好了。该代码在运行应用程序时运行良好,但不会在界面构建器中显示占位符文本。我还收到以下非阻塞错误(我认为这就是它在设计时不呈现的原因):
错误:IB Designables:无法更新自动布局状态:Interface Builder Cocoa Touch Tool 崩溃
错误:IB Designables:无法渲染 PlaceholderTextView 的实例:渲染视图耗时超过 200 毫秒。您的绘图代码可能会遇到性能缓慢的问题。
我无法弄清楚导致这些错误的原因。第二个错误没有任何意义,因为我什至没有覆盖 drawRect()。有什么想法吗?
【问题讨论】:
-
我收到了这个错误。该对象在测试项目中运行良好,但在我的主项目中(在表格中)却不行。根据developer.apple.com/library/mac/recipes/…,我选择了Choose Editor > Debug Selected Views。然后我得到“无法调试视图”“确保您的框架具有为 iOS 构建的正确构建设置。” Google 中没有出现此错误。
标签: ios swift xcode storyboard interface-builder