我最近一直在开发一款新应用,而且我一直有这个奇怪的问题。每当我尝试访问viewdidload之外的textviews.text属性时,我都会收到错误“实例成员'detailDescriptionLabel'(我的textview)不能用于'DetailViewController'类型(我上课)”我真的很赞赏你的帮助。感谢。
出现错误的代码:
var updated: String = detailDescriptionLabel.text {
didSet {
if updated != oldValue {
textFieldDidChange()
}
}
}
整个班级,控制桌子上的单元格(只是加入):
var start : CGPoint?
var red : CGFloat = 0.0
var green : CGFloat = 0.0
var blue : CGFloat = 0.0
@IBAction func erase(sender: AnyObject) {
red = 1
green = 1
blue = 1
}
@IBAction func clear(sender: AnyObject) {
self.imageView.image = nil
}
@IBAction func blackPen(sender: AnyObject) {
red = 0
green = 0
blue = 0
}
@IBAction func redPen(sender: AnyObject) {
red = 1
green = 0
blue = 0
}
@IBAction func bluePen(sender: AnyObject) {
red = 0
green = 0
blue = 1
}
@IBOutlet weak var detailDescriptionLabel: UITextView!
@IBOutlet weak var imageView: UIImageView!
let notes = PFObject(className: "Notes")
var detailItem: AnyObject? {
didSet {
// Update the view.
self.configureView()
}
}
func configureView() {
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.configureView()
}
var updated: String = detailDescriptionLabel.text {
didSet {
if updated != oldValue {
textFieldDidChange()
}
}
}
func textFieldDidChange() {
let query = PFQuery(className: "Notes")
query.getObjectInBackgroundWithId("Content") { (notes, error) -> Void in
notes!["Content"] = self.detailDescriptionLabel.text
print("Updated")
}
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
let newText = change![NSKeyValueChangeNewKey] as! String
print(newText)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func draw(start : CGPoint, end: CGPoint) {
UIGraphicsBeginImageContext(self.imageView.frame.size)
let context = UIGraphicsGetCurrentContext()
imageView?.image?.drawInRect(CGRect(x: 0, y: 0, width: imageView.frame.width, height: imageView.frame.height))
CGContextSetLineWidth(context, 6)
CGContextBeginPath(context)
//Define starting point
CGContextMoveToPoint(context, start.x, start.y)
//Define end point
CGContextAddLineToPoint(context, end.x, end.y)
//Close the path between the starting and ending point (draw)
CGContextSetRGBStrokeColor(context, red, green, blue, 1)
CGContextStrokePath(context)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
imageView.image = newImage
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let Touch = touches.first as UITouch!
start = Touch.locationInView(self.imageView)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let Touch = touches.first as UITouch?
let end = Touch?.locationInView(self.imageView)
if let s = self.start {
draw(s, end: end!)
}
self.start = end
}
答案 0 :(得分:0)
var myString: String {
didSet {
if newValue != oldValue {
//what ever you wanna do here.
}
}
}
试试这段代码。还有一个&#34; newValue&#34;可以用的。
就像我说的,当UITextView.text更改文本时,使用此代码进行更新。
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
myString = (textView.text+text)
return true
}
答案 1 :(得分:0)
似乎不允许以这种方式设置成员变量...您需要实现textViewDidChange委托方法