标签:
第一种:继承于UITabBarController的自定义
- (void)viewDidLoad
{
[super viewDidLoad];
[self creatViewController];
self.tabBar.hidden =YES;//隐藏系统Tabbar
//方法一种的以下控件不建议用xib拖拽;
UIView *smallView =[[UIView alloc] initWithFrame:CGRectMake(0, 518, 320, 50)];
smallView.backgroundColor =[UIColor redColor];
[self.view addSubview:smallView];
[smallView release];
UIImageView *smallImageView =[[UIImageView alloc] initWithFrame:smallView.bounds];
smallImageView.image =[UIImage imageNamed:@"tabBar"];
[smallView addSubview:smallImageView];
[smallImageView release];
// x = row +(row+kuang)*(i%3)
// y = lierow +(lierow+gao)*(i/3)
for (int i =0 ;i<5;i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame =CGRectMake(20+60*i, 5, 40, 40);
btn.tag =i+1;
[btn setImage:[UIImage imageNamed: [NSString stringWithFormat:@"%d.png",i+1]] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnCilck:) forControlEvents:UIControlEventTouchUpInside];
[smallView addSubview:btn];
}
}
-(void)creatViewController
{
UIViewController *viewController1 =[[UIViewController alloc] init];
viewController1.view.backgroundColor =[UIColor redColor];
UIViewController *viewController2 =[[UIViewController alloc] init];
viewController2.view.backgroundColor =[UIColor blueColor];
UIViewController *viewController3 =[[UIViewController alloc] init];
viewController3.view.backgroundColor =[UIColor blackColor];
UIViewController *viewController4 =[[UIViewController alloc] init];
viewController4.view.backgroundColor =[UIColor greenColor];
UIViewController *viewController5 =[[UIViewController alloc] init];
viewController5.view.backgroundColor =[UIColor grayColor];
self.viewControllers=@[viewController1,viewController2,viewController3,viewController4,viewController5];
[viewController1 release];
[viewController2 release];
[viewController3 release];
[viewController4 release];
[viewController5 release];
}
-(void)btnCilck:(UIButton *)btn
{
self.selectedIndex=btn.tag-1;
}
@end
第二种 继承于UIViewController 的自定义
@interface CustomViewController : UIViewController
@property (nonatomic,retain)NSArray *viewControllers;
- (IBAction)btnCilck:(id)sender; //xib连线方法声明
@property (nonatomic,assign)NSInteger selectedIndex;
@end
//xib中的控件可与第一种方法中一样手写
- (void)viewDidLoad
{
[super viewDidLoad];
[self creatViewController];
if (_selectedIndex>[self.viewControllers count]||_selectedIndex<0)
{
_selectedIndex =0;
}
//根据索引显示当前页面
UIViewController *viewController1 =self.viewControllers[_selectedIndex];
[self.view addSubview: viewController1.view];
//视图放在底层,避免遮盖TabBar;
[self.view sendSubviewToBack:viewController1.view];
}
-(void)creatViewController
{
UIViewController *viewController1 =[[UIViewController alloc] init];
viewController1.view.backgroundColor =[UIColor redColor];
UIViewController *viewController2 =[[UIViewController alloc] init];
viewController2.view.backgroundColor =[UIColor blueColor];
UIViewController *viewController3 =[[UIViewController alloc] init];
viewController3.view.backgroundColor =[UIColor blackColor];
UIViewController *viewController4 =[[UIViewController alloc] init];
viewController4.view.backgroundColor =[UIColor greenColor];
UIViewController *viewController5 =[[UIViewController alloc] init];
viewController5.view.backgroundColor =[UIColor grayColor];
self.viewControllers=@[viewController1,viewController2,viewController3,viewController4,viewController5];
NSLog(@"%ld",self.viewControllers.count);
[viewController1 release];
[viewController2 release];
[viewController3 release];
[viewController4 release];
[viewController5 release];
}
//系统方法,设置选中的索引
-(void)setSelectedIndex:(NSInteger)selectedIndex
{
if (selectedIndex ==_selectedIndex) {
return;
}
else
{
//移除之前加载的视图
UIViewController *viewController =self.viewControllers[_selectedIndex];
[viewController.view removeFromSuperview];
_selectedIndex =selectedIndex;
UIViewController *viewController1 =self.viewControllers[_selectedIndex];
[self.view addSubview: viewController1.view];
[self.view sendSubviewToBack:viewController1.view];
}
}
- (IBAction)btnCilck:(id)sender
{
UIButton *btn =(UIButton *)sender;
self.selectedIndex =btn.tag-1
;
}
@end
标签:
原文地址:http://www.cnblogs.com/LkBolg/p/4964113.html