Welcome to the Treehouse Community
Looking to learn something new?
Jacob Evans2,979 Points
Quantity not completely resetting when selecting a different item?
First I select an item and change quantity then I change my mind and select a new item.
Counter resets on screen but when you click plus it just adds +1 to the original quantity of the previously selected item stepper count.
Jesse DeOms18,815 Points
Hey Jacob, I was having the same issue!
My reset function sets my quantity variable to 1 when I switch items, but then when I try to increment or decrement the quantity using the buttons in the app, the quantity jumps to the stored sender.value (which is not being reset to 1 when I reset my quantity variable to 1)...
The best way that I have figured to fix this (after some serious googling) is to create a reference outlet for the button in our ViewControler file. We can use this reference outlet to assign a value of 1.0 to the counterValue as a part of our reset function ...
- Go to the assistant editor (the ven-diagram-ish double circle button in the top right of your screen)
- Open your Main.storyboard file on the left side of your screen and your ViewController file on the right side
- Control click on your Stepper button in the Main.storyboard file
- Select and drag Referencing Outlet to your View Controller file (give this UIStepper object the name "counterValue")
- In your reset function add the following line of code: counterValue.value = 1.0
I hope this helps!
Charlie O'Shea18,737 Points
Yep this is quite an issue in the app and would have been a useful way to demonstrate IBOutlets for controls.
I think there's another problem with the quantity code but I'm not sure yet if it gets address later on in the course. It only emerges when testing out the machine. For example if you attempt to buy 2 sandwiches straight away it will give you an insufficient funds error, but it will also decrease the quantity of the sandwich stock by 2 without completing the purchase. If the user then tries to buy the 1 sandwich they've been informed that they can afford it will tell them Out of Stock.
I've fixed it by moving
item.quantity -= quantity inventory.updateValue(item, forKey: selection)
to the bottom of the vend() function, or at least below the if block that tests if theres enough money deposited so the function throws the InsufficientFunds error before decrementing the quantity.
Jacob Evans2,979 Points
I actually figured this out while at WWDC with the swift engineers.
The solution was pretty much what you said. I had to add this line to the top of ViewController: @IBOutlet weak var stepperQuantity: UIStepper!
And connect it to the stepper.
Then I reset that to 1.0 in the rest method: stepperQuantity.value = 1.0
However, was this ever mentioned in any of the videos? I could not find it or completely missed this step. I also tried looking in the project files and that stepper connection was not in any of the files I tried.
May need to update the video or add into the teacher's notes.
Glad you added the steps and answered the question because now it should show up in Google results.