tabbarviewcontroller标签栏视图控制器
2015-04-12 10:59
253 查看
标签栏视图控制器是容器视图控制器的一种,每个界面都有一个视图控制器,可以是UIviewcontroller,UI tableviewcontroller等基本视图控制器。
例子:
定义musiclist按钮及界面(tableview controller):
- (id)initWithStyle:(UITableViewStyle)style{
在应用程序委托类中定义tabbarcontroller并将上面定义的两个视图控制器加到tabbarcontroller里。
文件代码:点击打开链接
例子:
定义musiclist按钮及界面(tableview controller):
- (id)initWithStyle:(UITableViewStyle)style{
<span style="font-size:18px;"> self = [super initWithStyle:style]; if (self) { self.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"MusicList" image:[UIImage imageNamed:@"Unlock"] tag:0];//根据tabbaritem设置按钮图片及按钮底部文本 } return self; } </span>
<span style="font-size:18px;">- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1;//设置节(section)的数目 } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10;//设置每节当中的行(Rows)数 } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; } cell.textLabel.text = [NSString stringWithFormat:@"Music %i",(indexPath.row+1)];// 设置每行的标题 return cell; }</span>定义currentlist的按钮及界面(viewcontroller):
<span style="font-size:18px;">-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if(self){ self.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"currentplay" image:[UIImage imageNamed:@"light"] tag:1]; //根据tabbaritem设置按钮图片及底部文字 } return self; } </span>
<span style="font-size:18px;">- (void)viewDidLoad { [super viewDidLoad]; //界面中的按钮 UIButton*btnplay=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btnplay.frame=CGRectMake(20, 180, 80, 60); [btnplay setTitle:@"PLAY" forState:UIControlStateNormal]; UIButton*btnstop=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btnstop.frame=CGRectMake(180, 180, 80, 60); [btnstop setTitle:@"STOP" forState:UIButtonTypeCustom]; [self.view addSubview:btnplay]; [self.view addSubview:btnstop]; } </span>
在应用程序委托类中定义tabbarcontroller并将上面定义的两个视图控制器加到tabbarcontroller里。
<span style="font-size:18px;">self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; //生成各个视图器的对象 MusicList*musiclist=[[MusicList alloc] init]; CurrentPlay*currentplay=[[CurrentPlay alloc] init]; //将对象放入到数组中 NSArray*ControllerArray=[[NSArray alloc]initWithObjects:musiclist,currentplay, nil]; UITabBarController*tabbarController=[[UITabBarController alloc]init];//创建tabbarcontrollerko控制器 tabbarController.delegate=self;//委托设置 tabbarController.viewControllers=ControllerArray;//设置tabbarcontroller的viewconreoller属性为之前生成的数组</span><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13px; font-family: Menlo; color: rgb(0, 143, 0);"> </p> tabbarController.selectedViewController=currentplay;//选择呈现哪个视图 self.window.rootViewController=tabbarController;//设置tabbarcontroller为根视图控制器
文件代码:点击打开链接
相关文章推荐
- UITabBarController作为根视图控制器的时候出现的问题,popToRootViewController的时候出现两个TabBar
- TabBarViewController标签导航控制器全局控制
- iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)
- CodeIgniter框架模型(model),视图(view)和控制器(controller)中的文件操作
- ASP.NET MVC中从后台控制器(Controller)传递数据到前台页面视图(View)方式
- 表格视图UITableView及控制器UITableViewController
- MVC(Model View Controller)模型-视图-控制器
- iOS7应用开发5、视图控制器View Controller及其生命周期
- 视图篇——表格视图UITableView及控制器UITableViewController
- iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)
- 七、MVC之 控制器controller与视图view
- iOS7新特性 ViewController转场切换(三) 自定义视图控制器容器的切换---非交互式
- iOS7应用开发5、视图控制器View Controller及其生命周期
- ASP.NET MVC中从前台页面视图(View)传递数据到后台控制器(Controller)方式
- ASP.NET MVC中从前台页面视图(View)传递数据到后台控制器(Controller)方式
- iOS开发:第一个iOS程序分析——AppDelegate.h文件和视图View、视图控制器ViewController
- MVC(Model View Controller)模型-视图-控制器
- IOS Note - View Controller(视图控制器)
- 视图篇——表格视图UITableView及控制器UITableViewController
- ipad UISplitViewController 导航视图控制器标题设置