K. Airewele K. Airewele - 5 months ago 24
iOS Question

How do I check a UILabel text against a string value?

I'm not a very experience coder so I am having difficulty figuring out if it's possible and if so how to check the string found in a label against a given string value.

I want my Add button to be enabled when the Radius Label says something other than "Radius in feet" and when my Message Text Field is NOT empty.

So far I have:

@IBAction func textFieldEditingChanged(sender: UITextField) {
addButton.enabled = !radiuslabel.text == "Radius in feet" && !messageTextField.text.isEmpty
}


This code is wrong and I have been unable to find an answer. The Radius label is being given information from a slider.
This is the code for the slider function:

@IBAction func sliderValueChanged(sender: UISlider) {
var currentValue = Int(sender.value)
radiusLabel.text = "\(currentValue)"
}


Is there another way to write this that keeps the Radius as a Integer while part of the Label?

Answer

Let us suppose your slider has value between 1 to 10 and 2 represent the value in Feet

so according to your code, you are setting the value in radiusLabel when slider value change

@IBAction func sliderValueChanged(sender: UISlider) {
    var currentValue = Int(sender.value)
    radiusLabel.text = "\(currentValue)"
}

AND changing the value in Message Label

@IBAction func textFieldEditingChanged(sender: UITextField) {
    addButton.enabled = !radiuslabel.text == "Radius in feet" && !messageTextField.text.isEmpty
}

You are actually enabling the button when the value of messageTextField change

What you want is to enabled the button as soon as slider value set to feet or messageLabel has some value. You need to do is

func shouldEnableButton()
{
    //Assuming value 2 means value in feet
    addButton.enabled = radiuslabel.text != "2" && (messageTextField.text != nil && !messageTextField.text!.isEmpty)
}

@IBAction func sliderValueChanged(sender: UISlider) {
    var currentValue = Int(sender.value)
    radiusLabel.text = "\(currentValue)"
    messageLbl.text = nil
    self.shouldEnableButton()
}

@IBAction func textFieldEditingChanged(sender: UITextField) {
    self.shouldEnableButton()
}
Comments