|
I've been figuring out how to share the color data to other views but still haven't been able to do it. What I wanna do here is by selecting your prefered color in the color setting view, for example, the color of the background of your content view or the color of the button in your another view also changes into the color you selected. I'd really appreciate your help.
|
|
Hi, I would suggest you use @EnvironmentObject (https://www.hackingwithswift.com/quick-start/swiftui/how-to-use-environmentobject-to-share-data-between-views). I’ve changed your code a little bit so that ColorSetting appears as a sheet. ColorTheme and ColorData don’t need to be changed.
Move the initialization of ColorTheme from ColorSetting to the @main structure.
Use @EnvironmentObject to access the color.
This code doesn’t load the saved color automatically, if you wish to do so you could write something like this:
Hope this helps! |
|
@sodapool Thank you so much for your answer, it absolutely helped :)! Now my color setting is working greatly, I'm happy with the result! Btw, I have another question if you could possibly answer. I only have a vague understanding on how the @main class(?) works, that is in the file that we always have every time we create a project. I would appreciate it if you could provide me some explanation on how it actually works :) |
|
I’m glad I could help. To answer your question about @main: Every program needs an entry point, i.e. a point where the execution begins. Take a playground as an example. Its code is executed from top to bottom, that’s because a playground consists of one main playground file and this file is the entry point. If there’s just one file, there’s no confusion about where to start. Apps consist of multiple files, it’s therefore necessary to mark the entry point. That’s the job of @main. @main itself is an attribute. Attributes provide additional information about a type. In our case @main marks the structure as providing the program’s entry point. (I previously wrote @main class which is wrong, it’s a structure. I’ve edited my answer.) @main on its own only indicates the location of the entry point, it doesn’t initialize or run the app. That’s done by a static method called main() and every type annotated with the @main attribute is required to declare it. We don’t need to do this, because the App protocol to which the structure conforms to does. The function takes care of the whole application startup. We however, don’t know a lot about it, because the SwiftUI framework isn’t open source and this is all we get to see.
|
|
@sodapool I really appreciate your clear answer :) Now I get how @main structure works and why it's necessary to mark an entry point :) Thank you so much for your time to answer the question! |
SAVE 50% To celebrate Black Friday, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
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.