|
Hi all. I've been making good progress in learning SwiftUI, having completed the first 3 projects in Hacking with iOS, including all the challenges so far, but am completely stumped on the 3rd challenge of project 4 (BetterRest): https://www.hackingwithswift.com/books/ios-swiftui/betterrest-wrap-up The challenge is: "Change the user interface so that it always shows their recommended bedtime using a nice and large font. You should be able to remove the “Calculate” button entirely." Removing the calculate button and displaying the text is easy, but I don't know how to have it always show the result in the text when you adjust the other controls (instead of clicking the calculate button to show the result in an alert) I have searched the internet and it seems like Binding could solve the problem, but am failing to implement this into the project - because I'm not sure if this is the solution, and also struggling to understand how Binding works (and even where I'd put the code). Any help would be much appreciated! Here is the completed project, without any alterations:
|
|
|
|
Thanks for your reply. I had already tried this, but found that although it helps with the Picker (as the view changes, so onAppear gets called when you return to the original view), it doesn't help when you change the Stepper or DatePicker values. |
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts! Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
Yeah correct, ok I've checked again my solution and remembered that I could get it working with
And I'm using it in
Whenever I update desired amount of sleep, wake up time, or daily coffee intake it get updated automatically. |
|
Thank you so much, that works beautifully. I think I was overthinking it and going down a far more complicated route than I needed to! |
|
@Sangsom is there way to center align this section ?
I used HStack to be able to do that ... couldnt do with Section
to center align it. |
|
@ioprojecton If it helps, I got to center align Text( ) inside a Section, using .frame modifier like this:
|
|
This may be a totally different approach but I wanted to ask anyway.. could we possibly implement this using computed properties.. ie. switch the calculateBedTime function into a computed property >.> |
|
I moved the calculateBedTime calculations to a computed property of type String and used that to display to the user. Seems to work |
|
|
|
Hello, here is one solution for Stepper, I don't know is it the best but it works just fine
|
|
I can't get my bedtime to update automatically. I use the instructor's code and made slight adjustments and cannot get the bedtime value to appear. I get the following error: Modifying state during view update, this will cause undefined behavior. Here is my code:
|
|
Making a computed property called bedTime call calculateBedtime() resolve my issue. Now it automatically updates. |
BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.