iOS中NSNotificationCenter…

来源:互联网 发布:parsley.min.js 编辑:程序博客网 时间:2024/06/05 19:21
原文地址:iOS中NSNotificationCenter实现主题背景更换作者:伤心的小果冻
创建一个baseViewController 
然后所有的子视图都继承于该视图控制器,主题思想是baseViewController的背景颜色改变后,所有的子视图控制器的背景颜色也随之改变

在想修改主题背景的地方加入以下代码

[[NSNotificationCenterdefaultCenter]postNotificationName:@"BGCOLOR_CHANGED" object:[UIColor colorWithRed:rgreen:g blue:balpha:1]];



r,g,b是你修改的颜色的三基色值(可以用slider设置相应的r,g,b值)

然后在baseViewController的init方法中添加以下代码:

[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(setBGColor:) name:@"BGCOLOR_CHANGED" object:nil];


并添加方法

 

-(void)setBGColor:(NSNotification *)noti

{

    UIColor * color = noti.object;

   self.view.backgroundColor = color;

}