UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

DatePicker - refresh.

Forums > SwiftUI

Hello,

I have an app that works out ETA, fuel consumption etc for a ship. After minimising the view, then maxmising the view, the date on the date picker does not update to the current time. Closing the app then opening it refreshes the date. It would be nice every time you open the view that the Date picker shows the current time and date. It only does this when you launch the app.


 @State private var eTD = Date ()

  DatePicker("ETD: ", selection: $eTD)

I tried this:

 .onAppear() {
                            self.eTD = Date()
                    }

It works but Swift complains -

[TableView] Warning once only: UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window). This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (e.g. table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes

Any recommendations much appreciated!

2      

Hi PJCCMC,

If you look to the bottom of SceneDelegate.swift, you'll find 4 "empty" methods with descriptions.

You can use sceneWillEnterForeground to refresh your eTD property.

2      

Thank you ! - but Scene Delegate did not have a function called sceneWillEnterForeground. Perhaps - func sceneDidBecomeActive(_ scene: UIScene)?

2      

@baduxdaniel, if it is a @State private how do I refresh it in SceneDelegate? Should I convert it into an @EnvironmentObject?

2      

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.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.