Group Group Group Group Group Group Group Group Group

Chapter 4 - UI Controls - WKInterfaceLabel not updating weight at runtime

#1

I’m not sure how to fix this. If I debug, weight is being incremented on Plus and Minus button press. The label however is not.
Swift 4
Xcode 9.2

I have implemented the onMinus and onPlusButtons

they either decrement or increment the var weight by 1 and then calls updateConfiguration()

        cookLabel.setText(cookTemp.stringValue)
        timerUpdate()
        convertToWeight(value:(usingMetric ? "grams" : "ounce"))
        currentCoolWeightLabel.setText("Weight: \(weight) \(unit)")
    }

When I first start the app in the simulator, the following does update the label

 currentCoolWeightLabel.setText("Weight: \(weight) \(unit)")

however the label doesn’t update after pressing either buttons, however the value of weight does.

how do I debug this?

I’ve included my full InterfaceController.swift file.

import WatchKit
import Foundation

class InterfaceController: WKInterfaceController {

    @IBOutlet weak var timer: WKInterfaceTimer!
    
    @IBOutlet weak var currentCoolWeightLabel: WKInterfaceLabel!
    
    @IBOutlet var timerButton: WKInterfaceButton!
    
    @IBOutlet var cookLabel: WKInterfaceLabel!
    //stables
    var weight = 16
    var ounces = 16
    var unit = "g"
    var grams = 500
    var usingMetric = true;
    var timerRunning = false
    //Rare = 0, Medium Rare = 1, medium = 2, well dont = 3
    var cookTemp = MeatTemperature.medium
    override func awake(withContext context: Any?) {
    super.awake(withContext: context)
        updateConfiguration()
    }
    
    func convertToWeight(value: String){
        if value=="ounce" {
            let ounce = Double(grams) /  28.3495
            weight = Int(ounce)
            unit = "oz"
            
        }else{
            let gram = Double(ounces) *  28.3495
            weight = Int(gram)
            unit = "g"
        }
        
    }
    func updateConfiguration() {
        cookLabel.setText(cookTemp.stringValue)
        timerUpdate()
        convertToWeight(value:(usingMetric ? "grams" : "ounce"))
        currentCoolWeightLabel.setText("Weight: \(weight) \(unit)")
        
    }
    @IBAction func onMetricChanged(_ value: Bool) {
        usingMetric = !usingMetric;
        updateConfiguration()
    }
    @IBAction func onTimerButton() {
        if(timerRunning){
            timer.stop()
            timerButton.setTitle("Start Timer");
        }else{
            timerUpdate()
            timer.start();
            timerButton.setTitle("Stop Timer");
            
            scroll(to: timer, at: .top, animated: true)
        }
        
        timerRunning = !timerRunning
    }
    
    @IBAction func onMinusButton() {
        weight -= 1
        updateConfiguration()
    }
    
    @IBAction func onPlusButton() {
        weight += 1
        updateConfiguration()
    }
    func timerUpdate(){
        //TODO move to method so countdown time changes when you change temperature.
        //“Set countdown based on weight"
        let countdown: TimeInterval =
            usingMetric
                ?cookTemp.cookTimeForGrams(weight)
                :cookTemp.cookTimeForOunces(weight)
        //“You set the date for the timer and then start the timer. A timer won’t do anything until you call start() on it. Any time that passes between setting the date and calling start() will be subtracted from the timer.”
        timer.setDate(Date(timeIntervalSinceNow: countdown))
    }
    //only change label on temperature update
    @IBAction func onTemperatureChange(_ value: Float) {
        if let temp = MeatTemperature(rawValue:Int(value)){
            cookTemp = temp
            updateConfiguration()
        }
    }
    //implement overrides for scroll points
    override func interfaceOffsetDidScrollToTop() {
        print("User scrolled to top")
    }
    
    override func interfaceDidScrollToTop() {
        print("User went to top by tapping status bar")
    }
    
    override func interfaceOffsetDidScrollToBottom() {
        print("User scrolled to bottom")
    }
}
#2

Hello @ashstampede, thanks for reaching out.
actually the problem is quite simple :] you are indeed updating the weight value, but before updating the label you are resetting weight. convertToWeight(value:) is setting weight from either gram or ounce. and I can’t see those changing from the code you sent.

#3

Thanks @ehabamer, I didn’t register that. Have move change weight to only happen onMetricChanged. But at lease it works.

closed #4

This topic was automatically closed after 166 days. New replies are no longer allowed.