D
D
Denis2019-01-25 21:11:59
Swift
Denis, 2019-01-25 21:11:59

Label.text is not updated, I don’t understand - what’s the matter?

I can't understand what's the matter - if indicatorUnitPressed = 0, then when the button is pressed, the text changes without problems, but if indicatorUnitPressed is equal to a number from 1 to 7, then the first time the button is pressed, the text does not change, and the second time it changes.

var indicatorUnitPressed = 0 // Меняется от 0 до 7 в зависимости от того какую кнопку нажали
var switchPressed = 0 // по умолчанию


@IBAction func switchButtonPressed(_ sender: UIButton)
{
if switchPressed != 1
{
switch indicatorUnitPressed
{
case 0:
infoLabel.text = “Fº to Cº”
print("(indicatorUnitPressed)")
case 1:
infoLabel.text = “Km to Ml”
print("(indicatorUnitPressed)")
case 2:
infoLabel.text = “cm to in”
print("(indicatorUnitPressed)")
case 3:
infoLabel.text = “cm to ft”
case 4:
infoLabel.text = “m2 to sq.ft”
case 5:
infoLabel.text = “Litr to Gal”
case 6:
infoLabel.text = “Litr to Oz”
case 7:
infoLabel.text = “Kg to Pound”
default:
break
}
switchPressed = 1
}
else
{
print("(switchPressed)")
switch indicatorUnitPressed
{
case 0:
infoLabel.text = “Cº to Fº”
print("(indicatorUnitPressed)")
case 1:
infoLabel.text = “Ml to Km”
print("(indicatorUnitPressed)")
case 2:
infoLabel.text = “in to cm”
case 3:
infoLabel.text = “ft to cm”
case 4:
infoLabel.text = “sq.ft to m2”
case 5:
infoLabel.text = “Gal to Litr”
case 6:
infoLabel.text = “Oz to Litr”
case 7:
infoLabel.text = “Pound to Kg”
default:
break
}
switchPressed = 0
}
}

Full file here https://www.dropbox.com/s/fxa8ly6j5lxvpuq/MilesVie...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Babin, 2019-01-26
@chiliec

Try calling infoLabel.setNeedsDisplay()after the text has been assigned.
Any errors and warnings in the console?
It is possible that the execution is not on the main thread, then wrap it in

DispatchQueue.main.async { [unowned self] in
    infoLabel.text = text
}

K
Kyman, 2019-01-27
@Kyman

Did you look in the debug that switch enters the right branch?
Unfortunately, it's impossible to understand anything from your code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question