即使它的值似乎在代码中被更改,UIView也不会更新

时间:2015-11-25 19:45:40

标签: ios swift uiview

我正在尝试使用新值更新UIView(饼图)。在下面的方法中,我成功地将UIView的pieChart.counter值设置为我想要的值(并将其打印出来进行测试),但由于某种原因,饼图视图不会使用此值进行更新。

//MARK: Properties
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var pieChart: CounterView!
@IBOutlet weak var dateLabel: UILabel!

var healthData = HealthData()
let counterView = CounterView()
var scoreInt = 0 

override func viewWillAppear(animated: Bool) {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.healthData.updateHealthCharacteristics()
    let seconds = 0.1
    let delay = seconds * Double(NSEC_PER_SEC)  // nanoseconds per seconds
    let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

    dispatch_after(dispatchTime, dispatch_get_main_queue(), {
        self.scoreInt = Int(self.healthData.score)
        self.scoreLabel.text = String(self.scoreInt)
        self.counterView.counter = self.scoreInt
        self.pieChart.counter = self.counterView.counter
        print("pieChartValue: ", self.pieChart.counter)
        let dateString = self.formatDate(self.yesterday!)
        self.dateLabel.text = String(dateString)
    })
}

在我绘制饼图的类中,我有这段代码:

@IBDesignable class CounterView: UIView {

  let possiblePoints = 100
  let π:CGFloat = CGFloat(M_PI)

  @IBInspectable var outlineColor: UIColor = UIColor.blueColor()
  @IBInspectable var counterColor: UIColor = UIColor.orangeColor()

  var counter: Int = 0

  override func drawRect(rect: CGRect) {
    print("foo")
  }
}

我已经测试了drawRect方法,并且它工作正常,但是当我尝试使用我创建的pieChart.counter实例更新CounterView时似乎没有触发在我的viewController中。

1 个答案:

答案 0 :(得分:0)

要重绘某些视图,您只需在其上调用setNeedsDisplay即可。有关可以触发重绘的内容的详细信息,请参阅Drawing Concepts in iOS