IOS页面布局:
- #import "RootViewController.h"
- #import "FourViewController.h"
- @interface RootViewController ()
- @end
- @implementation RootViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self addChildViewClass:[OneViewController class] title:@"第一页" imageName:@"1"];
- [self addChildViewClass:[TowViewController class] title:@"第二页" imageName:@"1"];
- [self addChildViewClass:[ThreeViewController class] title:@"第三页" imageName:@"1"];
- [self addChildViewClass:[FourViewController class] title:@"第四页" imageName:@"1"];
- }
- - (void)addChildViewClass:(Class)class title:(NSString *)title imageName:(NSString *)imageName
- {
- UIViewController *viewC = [[class alloc] init];
- UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:viewC];
- navC.tabBarItem.title = title;
- navC.tabBarItem.image = [UIImage imageNamed:imageName];
- viewC.navigationItem.title = title;
- [self addChildViewController:navC];
- [navC release];
- [viewC release];
- }
- #import "FourViewController.h"
- @interface FourViewController ()
- @end
- @implementation FourViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- self.view.backgroundColor = [UIColor grayColor];
- UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
- button.frame = CGRectMake(100, 100, 100, 100);
- [button addTarget:self action:@selector(actionButton:) forControlEvents:(UIControlEventTouchUpInside)];
- button.backgroundColor = [UIColor redColor];
- [button setTitle:@"变色" forState:(UIControlStateNormal)];
- [self.view addSubview:button];
- [button release];
- }
- // 发送通知:通知的名字必须一样,否者接收不到
- - (void)actionButton:(UIButton *)button
- {
- [[NSNotificationCenter defaultCenter]postNotificationName:@"NOTICATIONONE" object:nil userInfo:@{@"wang":@"dong"}];
- }
- import "AppDelegate.h"
- #import "RootViewController.h"
- @interface AppDelegate ()
- @end
- @implementation AppDelegate
- - (void)dealloc
- {
- // 销毁通知
- [[NSNotificationCenter defaultCenter]removeObserver:self name:@"NOTICATIONONE" object:nil];
- [_window release];
- [super dealloc];
- }
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
- // Override point for customization after application launch.
- self.window.backgroundColor = [UIColor whiteColor];
- // 1,注册通知
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationInfo:) name:@"NOTICATIONONE" object:nil];
- RootViewController *rootVC = [[RootViewController alloc] init];
- self.window.rootViewController = rootVC;
- [rootVC release];
- [self.window makeKeyAndVisible];
- return YES;
- }
- // 2,接收通知的方法
- - (void)notificationInfo:(NSNotification *)notification
- {
- // 把wiodow的根视图控制器取出来
- UIViewController *viewC = self.window.rootViewController;
- //把已经设置window的rootVC 重置为空
- self.window.rootViewController = nil;
- // 3,设置皮肤(更改颜色)
- UINavigationBar *navBar = [UINavigationBar appearance];
- [navBar setBarTintColor:[UIColor redColor]];
- UITabBar *tabBar = [UITabBar appearance];
- [tabBar setBarTintColor:[UIColor redColor]];
- // 重新赋值window的根视图控制器
- self.window.rootViewController = viewC;
- // 接收通知里携带的参数
- NSDictionary *dic = notification.userInfo;
- NSLog(@"收到通知--%@ 通知名字--%@ 参数_-%@",dic,notification.name,notification.object);
- }