摘自:http://blog.csdn.net/yanghua_kobe/article/details/9555575
建立iOS项目时候,首先创建一个super class,以供其他viewController去继承。
以实现主题功能为例:BaseController里定义这个方法
- (void)configUIAppearance{
NSLog(@"base config ui ");
}
其他需要用到主题功能的viewController都继承BaseController,然后重写configUIAppearance方法,如:
- (void)configUIAppearance{
[self.sendButton setImage:...]
}
这样的好处有:
1、不用每个viewController都注册消息通知,因为共同的父类已经注册了(在BaseController的viewDidLoad里调用registerThemeChangedNotification方法进行注册)。
- (void)registerThemeChangedNotification{
[Default_Notification_Center addObserver:self
selector:@selector(handleThemeChangedNotification:)
name:Notification_For_ThemeChanged
object:nil];
}
2、不用在每个sub controller里显示调用configUIAppearance方法,因为在BaseController里会有代码调用。
消息处理的方法:
- (void)handleThemeChangedNotification:(NSNotification*)notification{
UIImage *navBarBackgroundImg=[[[ThemeManager sharedInstance] themedImageWithName:@"themeColor.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0.0f, 0.0f, 1.0f, 1.0f)
resizingMode:UIImageResizingModeTile];
[self.navigationController.navigationBar setBackgroundImage:navBarBackgroundImg
forBarMetrics:UIBarMetricsDefault];
[self configUIAppearance];
}
这里的[self configUIAppearance]的self实例其实是子类的实例,所以会去调用子类重写的方法。