简体   繁体   English

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

[英]Programatically created UILabel within UIView not updating

I have a UIView embedded within a UIViewController. 我有一个嵌入在UIViewController中的UIView。 The process is follows: 过程如下:

  1. UIView loads with parent UIVieController UIView与父UIVieController一起加载
  2. UIView class creates label and other graphics programatically UIView类以编程方式创建标签和其他图形
  3. UIViewController received a notification that a value has updated UIViewController收到值已更新的通知
  4. The UIViewController runs a func within the UIViews class UIViewController在UIViews类中运行一个func

The issue is the UIView label does not update with the value being sent to it. 问题是UIView标签不会随着发送给它的值而更新。 I have checked (see the print line) and the correct values is being received. 我已经检查(请参阅打印行),并且正在接收正确的值。

//  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)))")
    }
}
}

This is called from the parent UIViewController by: 这是通过父UIViewController通过以下方式调用的:

func eDataReceivedBLE(notification: NSNotification) {

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

    TemperatureUIView().setTemp(characteristic.value!)

}

And the notification within the UIViewController is: UIViewController中的通知是:

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

Any thoughts or guidance... 任何想法或指导...

You are creating a new view each time a notification is received, because you're calling TemperatureUIView().setTemp(characteristic.value!) . 每次收到通知时,您都在创建一个新视图,因为您正在调用TemperatureUIView().setTemp(characteristic.value!) (The () initializes a new instance, and you're making the call to the class, not an instance). ()初始化一个新实例,而您正在调用该类,而不是实例)。 This new instance is not being placed into your view hierarchy, so the old one just remains and it appears nothing happened at all. 这个新实例不会放入您的视图层次结构中,因此旧实例仍然存在,并且似乎什么也没有发生。

You should instead have a reference of some kind to your existing view, and on that, call existingTempView.setTemp(characteristic.value!) . 您应该改为对现有视图进行某种引用,然后在该视图上调用existingTempView.setTemp(characteristic.value!)

As an aside, you might be better off avoiding implementing the init functions, and instead implement awakeFromNib and call setup() there, as playing with init for views and view controllers can become confusing very fast. awakeFromNib ,您最好避免实施init函数,而应在其中实现awakeFromNib并调用setup() ,因为为视图和视图控制器使用init会变得非常混乱。

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

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