0

新手警告!: ) 我的应用程序中有多个视图,所有视图都设置在故事板中,并且都有自己的子类 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
} 

所以我用谷歌搜索和搜索,但就是不知道出了什么问题。是的,这是我使用 Swift 和 iOS 的第一周……

4

2 回答 2

0

这是您的电话updateModell

DrawGraph().updateModell()

此行创建一个新实例并将消息DrawGraph发送给它updateModell。这个新实例DrawGraph不在您的视图层次结构中。它与DrawGraph. 由于没有引用 的这个新实例,因此该实例在返回DrawGraph后立即被删除。updateModell你现在明白为什么这个调用对updateModell屏幕没有影响了吗?

我不知道您为什么决定尝试实现自己的Button课程。你有什么不喜欢的理由UIButton吗?

实现这一点的正常方法是将counter属性放在视图控制器中(或视图控制器引用的模型对象中)。视图控制器还引用了按钮和现有的DrawGraph. 将按钮的“Touches Ended”事件连接到IBAction视图控制器中的方法。在该IBAction方法中,更新计数器,更新按钮的标签(或其他),并发updateModell送到现有的DrawGraph.

于 2015-02-23T20:18:31.987 回答
0

在Storyboard上的View和DrawGraph之间创建一个outlet,假设你命名为DrawGraphV,确保view是Custom class DrawGraph,不需要updateModell方法,直接调用DrawGraphV.SetNeedsDisplay

于 2015-04-21T19:52:12.917 回答