您好我正在嘗試編寫一些帶有主題切換器的iPhone應用程序,用戶可以在其中選擇主題以更改背景顏色,Alpha,圖像以及一些按鈕的外觀和感覺(大小,圖像,甚至位置)。將主題應用到iPhone應用程序的最佳方式
應用主題的最佳方式是什麼?
感謝, 添
您好我正在嘗試編寫一些帶有主題切換器的iPhone應用程序,用戶可以在其中選擇主題以更改背景顏色,Alpha,圖像以及一些按鈕的外觀和感覺(大小,圖像,甚至位置)。將主題應用到iPhone應用程序的最佳方式
應用主題的最佳方式是什麼?
感謝, 添
還沒有把任何答案。我用事件和singlton來實現它。基本上,單例設置對象將更改分派給觀察者,觀察者根據事件更新GUI。 我記得有一種方法可以偵聽實例變量的變化,但忘記了如何。無論如何,我目前的做法對我來說都非常好。
以下是我如何實現在FemCal中更改主題的功能。我已經以代碼片段的形式包含了一些細節。
創建一個存儲顏色,圖像等的單例ThemeMgr類。在需要時獲取單例。
@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」作爲通知。
- (void)fireTimer:(NSTimer *)timer
{
NSNotification * themeChange = [NSNotification notificationWithName:@"ThemeChange" object:nil];
[[NSNotificationQueue defaultQueue] enqueueNotification:themeChange postingStyle:NSPostWhenIdle];
}
顯然,您將有一些用戶界面來選擇所需的主題。 在這種情況下,用戶選擇一個主題並在0.5秒後觸發fireTimer
。在強制UI重繪之前,這爲其他UI更新提供了很好的延遲。在任何需要針對主題更改採取行動的地方收聽通知。
// listen for change notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateAppearance:) name:@"ThemeChange" object:nil];
我只有幾個視圖,所以我在每個我使用的控制器中編寫了代碼,但是您可以使用objective-C的強大功能來混合代碼以更好地處理這個問題。實現代碼以實際重新繪製基於主題的視圖。
- (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中或類似代碼顯示在視圖之前,應用任何主題。