我正在尝试为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.
}
}
答案 0 :(得分:0)
问题似乎是您要将文本设置在方法或闭包范围之外。
试试这个:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Manually create a placeholder for the text view
pinContent.text = "Description" // This line is where I get my error
pinContent.textColor = UIColor.lightGrayColor()
println(userLocation)
}