您的位置:首页 > 其它

tabbarviewcontroller标签栏视图控制器

2015-04-12 10:59 253 查看
标签栏视图控制器是容器视图控制器的一种,每个界面都有一个视图控制器,可以是UIviewcontroller,UI tableviewcontroller等基本视图控制器。

例子:

定义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为根视图控制器


文件代码:点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐