繁体   English   中英

UIView中以编程方式创建的UILabel无法更新

[英]Programatically created UILabel within UIView not updating

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

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

问题是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!) ()初始化一个新实例,而您正在调用该类,而不是实例)。 这个新实例不会放入您的视图层次结构中,因此旧实例仍然存在,并且似乎什么也没有发生。

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

awakeFromNib ,您最好避免实施init函数,而应在其中实现awakeFromNib并调用setup() ,因为为视图和视图控制器使用init会变得非常混乱。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM