2010-04-28 68 views
4

您好我正在尝试编写一些带有主题切换器的iPhone应用程序,用户可以在其中选择主题以更改背景颜色,Alpha,图像以及一些按钮的外观和感觉(大小,图像,甚至位置)。将主题应用到iPhone应用程序的最佳方式

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

感谢, 添

回答

0

还没有把任何答案。我用事件和singlton来实现它。基本上,单例设置对象将更改分派给观察者,观察者根据事件更新GUI。 我记得有一种方法可以侦听实例变量的变化,但忘记了如何。无论如何,我目前的做法对我来说都非常好。

6

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

  1. 创建一个存储颜色,图像等的单例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; 
    }

  2. 使用通知来广播主题更改。我用@“ThemeChange”作为通知。

    - (void)fireTimer:(NSTimer *)timer 
    { 
    NSNotification * themeChange = [NSNotification notificationWithName:@"ThemeChange" object:nil]; 
    [[NSNotificationQueue defaultQueue] enqueueNotification:themeChange postingStyle:NSPostWhenIdle]; 
    }
    显然,您将有一些用户界面来选择所需的主题。 在这种情况下,用户选择一个主题并在0.5秒后触发fireTimer。在强制UI重绘之前,这为其他UI更新提供了很好的延迟。

  3. 在任何需要针对主题更改采取行动的地方收听通知。

    // listen for change notification 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateAppearance:) name:@"ThemeChange" object:nil];
    我只有几个视图,所以我在每个我使用的控制器中编写了代码,但是您可以使用objective-C的强大功能来混合代码以更好地处理这个问题。

  4. 实现代码以实际重新绘制基于主题的视图。

    - (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中或类似代码显示在视图之前,应用任何主题。

相关问题