简体   繁体   中英

Programatically created UILabel within UIView not updating

I have a UIView embedded within a UIViewController. The process is follows:

  1. UIView loads with parent UIVieController
  2. UIView class creates label and other graphics programatically
  3. UIViewController received a notification that a value has updated
  4. The UIViewController runs a func within the UIViews class

The issue is the UIView label does not update with the value being sent to it. 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:

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:

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!) . (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!) .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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