在 UIView 中以编程方式创建的 UILabel 未更新

Programatically created UILabel within UIView not updating

我在 UIViewController 中嵌入了一个 UIView。过程如下:

  1. UIView 加载父 UIVieController
  2. UIView class 以编程方式创建标签和其他图形
  3. UIViewController 收到一个值已更新的通知
  4. UIViewController 在 UIView 中运行一个函数 class

问题是 UIView 标签没有更新发送给它的值。我已经检查(参见打印行)并且收到了正确的值。

//  TemperatureUIView.swift

import UIKit

class TemperatureUIView: UIView {

var tempLabel : UILabel!

override init(frame: CGRect) {
    super.init(frame: frame)
    setup()
}

required init(coder: NSCoder) {
    super.init(coder: coder)!
    setup()
}

func setup(){
    drawBaseCircle()
    drawTempLabel()
}

func drawBaseCircle(){

    //Temperature Base Ring
    let baseCircle = CAShapeLayer()
    let baseCirclePath = UIBezierPath()
    let baseCircleRadius :CGFloat = (self.frame.height/2)-10

    baseCirclePath.addArcWithCenter(CGPoint(x: CGFloat(self.frame.width/2), y: CGFloat(self.frame.height/2)), radius: CGFloat(baseCircleRadius), startAngle: CGFloat(-M_PI_2), endAngle:CGFloat(M_PI_2*3), clockwise: true)
    baseCircle.path = baseCirclePath.CGPath
    baseCircle.fillColor = UIColor.clearColor().CGColor
    baseCircle.strokeColor = UIColor.lightGrayColor().CGColor
    baseCircle.lineWidth = 10.0
    self.layer.addSublayer(baseCircle)
}

func drawTempLabel(){
    tempLabel = UILabel(frame: CGRect(origin: CGPoint(x: 0, y:0), size: CGSize(width: 40, height: 40)))
    tempLabel.frame.origin = CGPoint(x: (self.frame.width / 2)-(tempLabel.frame.size.width/2), y: (self.frame.height / 2)-(tempLabel.frame.size.height/2))
    tempLabel.font = UIFont.boldSystemFontOfSize(15.0)
    tempLabel.textAlignment = NSTextAlignment.Center
    tempLabel.textColor = UIColor.whiteColor()
    tempLabel.tag = 101
    tempLabel.layer.name = "temperatureDisplay"
    self.addSubview(tempLabel)
    self.bringSubviewToFront(tempLabel)

    tempLabel.text = "---"
}

func setTemp(rawValue: NSData){

    var buffer8LSB : UInt8 = 0
    rawValue.getBytes(&buffer8LSB, range : NSMakeRange(5, 1))

    if (self.viewWithTag(101) != nil ){
        tempLabel.text = ("\(String(format: "%.0f", Float(buffer8LSB)))")
        print ("\(String(format: "%.0f", Float(buffer8LSB)))")
    }
}
}

这是从父 UIViewController 调用的:

func eDataReceivedBLE(notification: NSNotification) {

    let characteristic = notification.userInfo!["characteristic"] as! CBCharacteristic
    //let peripheral = notification.userInfo!["peripheral"] as! CBPeripheral

    TemperatureUIView().setTemp(characteristic.value!)

}

UIViewController 中的通知是:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(PluginMotionDataViewController.eDataReceivedBLE(_:)), name: "EdataReceived", object: nil)

任何想法或指导...

每次收到通知时您都在创建一个新视图,因为您正在调用 TemperatureUIView().setTemp(characteristic.value!)。 (() 初始化一个新实例,而您正在调用 class,而不是实例)。这个新实例没有被放入您的视图层次结构中,所以旧实例仍然存在,看起来什么也没发生。

您应该改为对现有视图进行某种引用,然后调用 existingTempView.setTemp(characteristic.value!)

顺便说一句,你最好避免实现 init 函数,而是实现 awakeFromNib 并在那里调用 setup(),就像玩 init 一样视图和视图控制器很快就会变得混乱。