簡體   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