好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

IOS学习笔记UITabBarController

实现一个简单的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 { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization self.title = @"首页"; self.tabBarItem.badgeValue = @"New";

} return self; }

-(void)loadView { UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; view.backgroundColor = [UIColor whiteColor]; self.view = view;

}

技巧: 给Iphone图标添加数字提示:self.tabBarItem.badgeValue = 8;  

查看更多关于IOS学习笔记UITabBarController的详细内容...

  阅读:30次