cs193p – Assignment #1 Extra Task #1

cs193p-assignment-1-extra-task-1-winter-2017

Implement a “backspace” button for the user to touch if they hit the wrong digit button. This is not intended to be “undo,” so if the user hits the wrong operation button, he or she is out of luck! It is up to you to decide how to handle the case where the user backspaces away the entire number they are in the middle of typing. You will probably find the Strings and Characters section of the Swift Reference Guide to be helpful here.

Add the backspace button to the storyboard:
cs193p-assignment-1-extra-task-1-winter-2017-storyboard

Link the new button to a new action method. When the user is in the middle of typing remove the last digit. If it is the last one set the display to zero:

    @IBAction func backSpace(_ sender: UIButton) {
        if userIsInTheMiddleOfTyping, var text = display.text {
            text.remove(at: text.index(before: text.endIndex))
            if text.isEmpty {
                text = "0"
                userIsInTheMiddleOfTyping = false
            }
            display.text = text
        }
    }

The complete code for the assignment #1 extra task #1 is available on GitHub.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

Leave a Reply

Your email address will not be published. Required fields are marked *