|
Another raw beginner's question, I'm afraid. I'm writing a calculator which needs an age to be entered. I want to be able to do it by entering the age itself or by entering a date of birth (DoB) from which I can calculate the age. To make entering the DoB easy, I've It works reasonably well. If I change the DoB using the keyboard, I can read it and update the age. If I then tap again on the DoB field, the picker appears with the chosen DoB shown. But if I enter an age, then calculate the DoB from it, the DoB display changes as I'd expect, but when I tap on the DoB field, the picker appears set to whatever date was previously chosen in the picker, not the DoB which is seen in the display. I'm obviously missing some step, which is passing the newly-calculated DoB down into the picker. I've tried various things but all have failed. I'm sure it must be easy. Help! Thanks. Jeremy |
|
I am guessing that you are using Difficult to see how to help, without seeing your code here, but when you enter the age, do you use it to update the @State variable DoB to corresponding to it? |
|
Fair point. The material bit of my code is pretty simple. Within the ContentView struct are
...
...
When calcDoB is called, it correctly calculates the date of birth from the entered age and the DateField changes to show the newly-calculated date of birth. The issue is that tapping the DateField brings up a picker which is set to whatever date was last entered using the picker, not the calculated (and displayed) dob. Jeremy |
|
Couple of changes for your code. First you need to update the
In my case I had to change the definition of
Add this function into the main struct
If this line fails during running
then change it to this
And finally use it like this, by changing the DateField operation.
|
|
Thanks for the help, but it doesn't work. The issue wasn't with updating ageText when changing the dob property: that was OK (I had a function calcAge, which was functionally identical to the one you've written, but I hadn't included it as I didn't think it mattered). The issue is the reverse: when the dob property is changed programmatically, in calcDoB(), the display of dob changes as expected; but when you then tap on the dob field, the date keyboard which appears has the last value set using the date keyboard, not the value set by dob. Try this:
The issue is with passing the correct, new value of dob into the datePickerView computed property of DateTextField. For some reason, it doesn't happen. Jeremy |
|
What is |
|
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.