【发布时间】:2015-03-28 23:26:00
【问题描述】:
我正在尝试为 UITextView 手动创建一些占位符文本,当我尝试设置占位符文本时,我收到 Swift 编译器错误。 Xcode 告诉我它需要一个 pinContent 声明,我首先尝试设置它的文本值。这是我的代码:
class FirstViewController: UIViewController {
@IBOutlet var pinTitle: UITextField!
@IBOutlet var pinContent: UITextView!
@IBAction func createPin(sender: AnyObject) {
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
println(userLocation)
}
// Manually create a placeholder for the text view
pinContent.text = "Description" // This line is where I get my error
pinContent.textColor = UIColor.lightGrayColor()
// Change the text properties of the text view when the user begins editing, so it types in the normal black font
func textViewDidBeginEditing(textView: UITextView) {
if textView.textColor == UIColor.lightGrayColor() {
textView.text = nil
textView.textColor = UIColor.blackColor()
}
}
// If the user leaves the text view blank when they're done editing, re-set the placeholder
func textViewDidEndEditing(textView: UITextView) {
if textView.text.isEmpty {
textView.text = "Description" // Sometimes I get the same error here as well
textView.textColor = UIColor.lightGrayColor()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
【问题讨论】:
-
将该代码移入方法中(例如移入 viewDidLoad)...
标签: swift compiler-errors uitextview declaration