将主题应用于iPhone应用程序的最佳方式

将主题应用于iPhone应用程序的最佳方式

问题描述:

您好我正在尝试使用主题切换器编写一些iPhone应用程序,用户可以选择主题来更改背景颜色,alpha,图像和一些按钮的外观和感觉(大小,图像,甚至地点)。

Hi I am trying to write some iPhone app with a theme switcher, where users can select a theme to change the background color, alpha, images, and some push buttons' look and feel as well (size, image, or even locations).

应用主题的最佳方式是什么?

What would be the best way to apply the theme?

谢谢,
Tim

Thanks, Tim

以下是我如何实现改变FemCal主题的能力。我已经以代码片段的形式包含了一些细节。

Here's how I implemented the ability to change themes in FemCal. I've included some of the details in the form of code snippets.


  1. 创建一个存储颜色,图像的单例ThemeMgr类等。在需要时获取单例。

  1. Create a singleton ThemeMgr class that stores colors, images etc. Fetch the singleton when needed.

@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;
}

使用通知广播主题更改。我用@ThemeChange作为通知。

Use notifications to broadcast theme changes. I used @"ThemeChange" as the notification.

- (void)fireTimer:(NSTimer *)timer
{
NSNotification * themeChange = [NSNotification notificationWithName:@"ThemeChange" object:nil];
[[NSNotificationQueue defaultQueue] enqueueNotification:themeChange postingStyle:NSPostWhenIdle];
}


显然,您可以使用一些UI来选择所需的主题。
在这种情况下,用户选择一个主题,并在0.5秒后触发 fireTimer 。在强制UI重绘之前,这为其他UI提供了一个很好的延迟。

Obviously, you'll have some UI to select a desired theme. In this case, the user chooses a theme and fireTimer is fired after 0.5 seconds. This gives a nice delay for other UI to update before I force the UI to redraw itself.

在需要对主题更改进行操作的任何地方收听通知。

Listen for notifications anywhere you need to act on a theme change.

// listen for change notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateAppearance:) name:@"ThemeChange" object:nil];


我只有几个视图,所以我在我使用的每个控制器中编写了代码,但你可以使用目标的力量 - C以混合代码来更好地处理这个问题。

I only have a few views, so I've written the code in each controller I use, but you can use the power of objective-C to mix code in to handle this in a better way.

实现代码以根据主题实际重绘视图。

Implement the code to actually redraw your view 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];
}

不要忘记在必要时重新签名任何通知,并且您必须在viewDidLoad或类似代码中编写代码以在显示视图之前应用任何主题。

Don't forget to resign any notifications when necessary, and you'll have to write code in viewDidLoad or similar to apply any themes before the view is displayed.