This is how I implemented the ability to change themes in FemCal. I have included some details in the form of code snippets.
Create a singleton ThemeMgr class that stores colors, images, etc. Choose a singleton if necessary.
@interface ThemeMgr : NSObject { // selected color and image NSString * selectedColor; NSString * selectedPicture; // dictionaries for color and image NSDictionary * colorData; NSDictionary * imageData; NSDictionary * backgroundData; // names NSArray * colors; NSArray * images; // themes UIColor * tintColor; UIImageView * panelTheme; UIColor * tableBackground; }
Use notifications to broadcast theme changes. As a notification, I used @ThemeChange.
- (void)fireTimer:(NSTimer *)timer { NSNotification * themeChange = [NSNotification notificationWithName:@"ThemeChange" object:nil]; [[NSNotificationQueue defaultQueue] enqueueNotification:themeChange postingStyle:NSPostWhenIdle]; }
Obviously, you will have some user interface to select the theme you want. In this case, the user selects a theme, and fireTimer starts after 0.5 seconds. This gives a good delay for updating another user interface before I force the user interface to redraw itself.Listen to notifications wherever you need to act when a topic changes.
// listen for change notification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateAppearance:) name:@"ThemeChange" object:nil];
I only have a few views, so I wrote the code in every controller that I use, but you can use the objective-C power to mix the code to better control this way.Embed the code to actually redraw your presentation based on the theme.
- (void)updateAppearance:(NSNotification *)notification { // background for grouped table view ThemeMgr * themeMgr = [ThemeMgr defaultMgr]; // change color and reload [self.tableView setBackgroundColor:[themeMgr tableBackground]]; [self.tableView reloadData]; self.navigationController.navigationBar.tintColor = [themeMgr tintColor]; }
Do not forget to cancel any notifications when necessary, and you will have to write code in viewDidLoad or similarly apply any themes before the view is displayed.
source share