Reputation: 137
I'm trying to create different themes for an app I'm creating in Xcode, with the latest version of Swift. So basically how I'm thinking to get it to work is
Why I'm asking this question is because I'm not sure how the other view controllers can access the variable 'theme' and its current value. Each view controller class is located in separate files.
I read some other answers to similar questions, but they don't seem to work. Any help would be appreciated
Upvotes: 3
Views: 2356
Reputation: 25
There are lot of solutions:
Other thing is how you refresh your current views/view controllers already pushed on the navigation controller. Inside viewDidAppear is a first approach, but in some cases you need to send a notification using Notification Center to all live instances of your view controllers/views to refresh. Here you have a nice example using a protocol for that purpose.
https://academy.realm.io/posts/architecting-a-robust-color-system-swift-tryswift-2017-ragone/
Upvotes: 2
Reputation: 100523
Share it with
option 1 Singleton
class Service {
static let shared = Service()
var theme:Theme = .black
}
option 2 global var
var theme:Theme = .black
and
enum Theme { case black,white }
regarding the refresh every vc should be configued to use this variable in worst case inside viewDidAppear
Upvotes: 4