)在我的应用程序中有多个视图,都是在故事板中设置的,并且都有自己的子类UIView类,它们都有自己的重写drawRect方法。其中之一作为一个按钮工作,并且能够通过setneedsdisplay重新绘制自己,并调用另一个UIViews类的函数:
import UIKit
class Button: UIView {
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
super.touchesEnded(touches, withEvent: event)
let touchPoint: CGPoint! = touches.anyObject()?.locationInView(self)
if butouched == true{
if touchPoint.x > bounds.size.width - bounds.size.height && touchPoint.x < bounds.size.width && touchPoint.y > 0 && touchPoint.y < bounds.size.height{
counter++
setNeedsDisplay() //thats the setNeedsDisplay that works
DrawGraph().updateModell()
}
}
}现在的问题是,这个函数中的setNeedsDisplay没有工作,尽管updateModell-函数被调用:
import UIKit
class DrawGraph: UIView {
func updateModell() {
setNeedsDisplay() // thats the setNeedsDisplay that doesn't work
} 所以我谷歌和谷歌,但就是找不出什么问题。是的,这是我在斯威夫特和iOS的第一周.
发布于 2015-04-21 19:52:12
在Storyboard上的视图和DrawGraph之间创建一个出口,假设您将它命名为DrawGraphV,确保视图是自定义类DrawGraph,您不需要方法updateModell,只需直接调用DrawGraphV.SetNeedsDisplay
发布于 2015-02-23 20:18:31
这是你打给updateModell的电话
DrawGraph().updateModell()这一行创建了一个新实例 of DrawGraph,并向它发送updateModell消息。DrawGraph的这个新实例不在您的视图层次结构中。它与DrawGraph的任何其他实例都是独立的。由于没有引用DrawGraph的新实例,因此在updateModell返回后立即删除该实例。您现在明白为什么对updateModell的调用对屏幕没有影响了吗?
我不知道为什么要尝试实现自己的Button类。你不喜欢UIButton有什么原因吗?
实现这一点的正常方法是将counter属性放在视图控制器(或视图控制器引用的模型对象中)中。视图控制器还具有对按钮和现有DrawGraph的引用。将按钮的“触摸结束”事件连接到视图控制器中的IBAction方法。在IBAction方法中,更新计数器,更新按钮的标签(或其他什么),并将updateModell发送到现有的DrawGraph实例。
https://stackoverflow.com/questions/28682252
复制相似问题