Make App Pie

Training for Developers and Artists

The Swift Swift Tutorial: Oops! Why your Pizza Prices Don’t Update

There are plenty of people who say if you make a mistake you will lose your reputation, so It’s best to hide your mistakes. I’m not one of those people. If I goof I’d rather get the situation fixed and get everything working again. I like that kind of transparency and integrity. So — I goofed. In my previous post on segues and delegates for the Pizza Demo app I made a goof. Annoyingly subtle mistake, but I goofed and the Pizza demo app delegates will look like they are not working right because of it. Good news — it has nothing to do with the delegates. I had found a bug while writing and forgot to write in the post how to fix it. So I apologize. I added the following to the original post. For those racking their brains trying to figure out why it isn’t working, I thought I’d post it again here so you know.

Again, my sincerest apologies.

There Is Always One More Bug

You might notice that you can change the prices of the pizzas by the + or but the delegate seemingly does not change the price with input from the text field. The reason is not the delegate but the text field. We have the wrong event for the target action. In the storyboard, right-click on the text field in Edit Price and you will see the default event of Editing Did End.
Screenshot 2014-07-11 09.54.52
Since there is no way to end editing on this view, we really want Editing Changed. Click the X next to the Pizza Type Price VC connection to Editing Did End to break the current connection. Bring up the assistant editor, and navigate to the PizzaTypePrice file if you need to. Right click the text field again and drag the circle for Editing Changed to the code for priceText(). Build and run and you should be able to change the price by the text field now.

Changing the price also needs to update the stepper’s value, so change priceText() to:

    @IBAction func priceText(sender: UITextField) {
        let priceString:NSString = sender.text
        pizzaPrice = priceString.doubleValue
        displayPizzaPrice()
        priceStepper.value = pizzaPrice
    }

One other change is far more cosmetic. Add this to the viewDidLoad() for PizzaTypePriceVC

priceText.becomeFirstResponder()

which will bring up the keyboard on load, and let you edit the price immediately with the keyboard.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: