實現一個簡單的UITabBar:
AppDelegate.m
代碼如下 復制代碼- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(320/2-140/2, 80, 140, 40)];
lab.text = @"王振";
lab.textAlignment = UITextAlignmentCenter;
lab.backgroundColor = [UIColor blueColor];
[self.window addSubview:lab];
//your code
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(320/2-140/2,480/2-40/2, 140, 40)];
label.text = @"Hello World";
label.backgroundColor = [UIColor cyanColor];
label.textAlignment = UITextAlignmentCenter;
[self.window addSubview:label];
HomeViewController *view1 = [[HomeViewController alloc]init];
UINavigationController *v1 = [[UINavigationController alloc]initWithRootViewController:view1];
MessageViewController *view2 = [[MessageViewController alloc]init];
UINavigationController *v2 = [[UINavigationController alloc]initWithRootViewController:view2];
SearchViewController *view3 = [[SearchViewController alloc]init];
UINavigationController *v3 = [[UINavigationController alloc]initWithRootViewController:view3];
SettingViewController *view4 = [[SettingViewController alloc]init];
UINavigationController *v4 = [[UINavigationController alloc]initWithRootViewController:view4];
//NSArray *arr = @[view1,view2,view3,view4];
NSArray *viewControllers = [NSArray arrayWithObjects:v1,v2,v3,v4 , nil];
UITabBarController *mainViewController = [[UITabBarController alloc]init];
mainViewController.viewControllers = viewControllers;
//[mainViewController setViewControllers:mainViewController];
//[mainViewController release];
[UIApplication sharedApplication].applicationIconBadgeNumber = 2;
[self.window setRootViewController:mainViewController];
return YES;
}
HomeViewController.m
代碼如下 復制代碼 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil技巧:
給Iphone圖標添加數字提示:self.tabBarItem.badgeValue = 8;