Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Andrés Leal
Andrés Leal
9,563 Points

Stop adding values SWIFT

I want to create an if statement that will see if there are 7 values, you should not enter more values through the buttons, and if there are less than 7 you are allowed to enter more values.

Chris Adamson
Chris Adamson
132,143 Points

Do you have some sample code to demonstrate what your trying to accomplish?

Andrés Leal
Andrés Leal
9,563 Points

I am trying to make an hexadecimal color app, so when you type the 6 values of the hex color it changes the background, I accomplish to write down the numbers but I can't stop the user form writing more values, also when you open the hex viewController it shows you a default value and I want that when the user taps on a new button when all the last 6 values of the hex color are added, it clears the hex value text label and you can write new 6 values. Here is my code:

@IBAction func buttonTapped(sender: AnyObject) {        
    if hexColor.text == "0" {
        hexColor.text = sender.titleLabel!!.text
    } else {
        hexColor.text = hexColor.text! + sender.titleLabel!!.text!
    }
    var hexString = hexColor.text

    if count(hexString!) == 7 {
        println("there are 6 values")
        colorWithHexString(hexString!)
    }
}

The "hexColor" is the text label to see what hexadecimal value is displayed, Also I want to include the "#" in before the number.

1 Answer

Hayden Pennington
Hayden Pennington
6,154 Points

Here is how I accomplished this.

// First conform to the UITextFieldDelegate,
class ViewController: UIViewController, UITextFieldDelegate {

    // Create an outlet to the textField
    @IBOutlet weak var textField: UITextField!

    // Also create an IBAction to the textField
    // Set the event to editing did begin
    // And the type to UITextField
    @IBAction func chage(sender: UITextField) {

        // Use string interpolation to add the #
        sender.text = "#\(sender.text)"
    }


    // Set the textFields delegate to self

    override func viewDidLoad() {
        super.viewDidLoad()

        textField.delegate  = self
    }


    // implement the 'shouldChangeCharactersInRange' delegate method
    // to prevent more than 7 characters (# + 6 characters) from being in the TextField.
    func textField(textField: UITextField, shouldChangeCharactersInRange
                       range: NSRange, replacementString string: String) -> Bool {

        // NSRange is a struct with two variable; location & length
        if range.location > 6 { return false }
        return true
    }

    // IBAction to your buton
    @IBAction func buttonTapped(sender: UIButton) {
        if count(textField.text) == 7 {

            colorWithHexString(textField.text)
            textField.text.removeAll(keepCapacity: false)
            textField.endEditing(true)
        }
    }

}

Of course you can set the placeholder text in Interface Builder.

Andrés Leal
Andrés Leal
9,563 Points

Hey? What if I am not using a textField and just a label to display the values, how do I endEditing??