完成基本功能之后 会重构代码 提高代码的健壮性
#import "AppDelegate.h"
#import "QHDiscoverViewController.h"
#import "QHHomeViewController.h"
#import "QHMessageCenterViewController.h"
#import "QHProfileViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//1.创建窗口
self.window = [[UIWindow alloc]init];
self.window.frame = [UIScreen mainScreen].bounds;
//2.设置根视图控制器
UITabBarController *tabbarVc = [[UITabBarController alloc]init];
self.window.rootViewController = tabbarVc;
QHHomeViewController *home = [[QHHomeViewController alloc]init];
[self addChildVc:home WithTitle:@"首页" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
QHMessageCenterViewController *messageCenter = [[QHMessageCenterViewController alloc]init];
[self addChildVc:messageCenter WithTitle:@"消息" image:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected"];
QHDiscoverViewController *discover = [[QHDiscoverViewController alloc]init];
[self addChildVc:discover WithTitle:@"发现" image:@"tabbar_discover" selectedImage:@"tabbar_discover_selected"];
QHProfileViewController *profile = [[QHProfileViewController alloc]init];
[self addChildVc:profile WithTitle:@"我" image:@"tabbar_profile" selectedImage:@"tabbar_profile_selected"];
// tabbarVc.viewControllers = @[vc1,vc2,vc3,vc4];
[tabbarVc addChildViewController:home];
[tabbarVc addChildViewController:messageCenter];
[tabbarVc addChildViewController:discover];
[tabbarVc addChildViewController:profile];
//很多重复的代码
//1.相同的代码放到一个方法中
//2.不同的东西变成参数
//3.在使用这段代码的这个地方调用方法传递参数
[self.window makeKeyAndVisible];
return YES;
}
/**
传控制器
*/
-(UIViewController *)addChildVc:(UIViewController *)childVc WithTitle:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
//删掉UIViewController *childVc = [[UIViewController alloc]init];
//设置子控件的文字图片
childVc.view.backgroundColor = QHRandomColor;
childVc.tabBarItem.title = @"我";
childVc.tabBarItem.image = [UIImage imageNamed:image];
childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//设置文字颜色
NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
textAttrs[NSForegroundColorAttributeName] =[UIColor colorWithRed:0.49f green:0.49f blue:0.49f alpha:1.00f];
[childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
NSMutableDictionary *selectedTextAttrs =[NSMutableDictionary dictionary];
selectedTextAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
[childVc.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateSelected];
[childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
[childVc.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateSelected];
childVc.view.backgroundColor = QHRandomColor;
return childVc;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
/**
* //3.设置子控制器
UIViewController *vc1 = [[UIViewController alloc]init];
vc1.tabBarItem.title = @"首页";
vc1.tabBarItem.image = [UIImage imageNamed:@"tabbar_home"];
//UIImage *homeSelectedImage = [UIImage imageNamed:@"tabbar_home_selected"];//自定义渲染成蓝色
//声明这张图片 以后按照原始的样子显示出来 不要自动渲染成其他颜色(比如蓝色)
// homeSelectedImage = [homeSelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//这个是新技术ios7
// vc1.tabBarItem.selectedImage = homeSelectedImage;
vc1.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_home_selected" ] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
vc1.view.backgroundColor = QHRandomColor;
//设置文字颜色
NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
textAttrs[NSForegroundColorAttributeName] =[UIColor colorWithRed:0.49f green:0.49f blue:0.49f alpha:1.00f];
[vc1.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
NSMutableDictionary *selectedTextAttrs =[NSMutableDictionary dictionary];
selectedTextAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
[vc1.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateSelected];
UIViewController *vc2 = [[UIViewController alloc]init];
vc2.tabBarItem.title = @"消息";
vc2.view.backgroundColor = QHRandomColor;
vc2.tabBarItem.image = [UIImage imageNamed:@"tabbar_message_center"];
vc2.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_message_center_selected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[vc2.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
[vc2.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateSelected];
UIViewController *vc3 = [[UIViewController alloc]init];
vc3.view.backgroundColor = QHRandomColor;
vc3.tabBarItem.title = @"发现";
vc3.tabBarItem.image = [UIImage imageNamed:@"tabbar_discover"];
vc3.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_discover_selected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[vc3.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
[vc3.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateSelected];
UIViewController *vc4 = [[UIViewController alloc]init];
vc4.view.backgroundColor = QHRandomColor;
vc4.tabBarItem.title = @"我";
vc4.tabBarItem.image = [UIImage imageNamed:@"tabbar_profile"];
vc4.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_profile_selected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[vc4.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
[vc4.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateSelected];
*/
@end
新浪微博开发-1
最新推荐文章于 2016-10-31 18:16:53 发布