iOS(四)动漫App:二

当第一次启动时,程序先跳转到firstViewController,之后跳转到ViewController,在ViewController上为了方便多个控制器跳转,所以需要用到UITabBarController,在这个App中需要在四个控制器中切换,Home主页;Classify分类;Search搜索;Private个人(个人这一块暂时没想到该怎么弄)


这是ViewController.h

让ViewController继承UITabBarController

<span style="font-size:18px;">#import <UIKit/UIKit.h>
@interface ViewController : UITabBarController

@end</span>
</pre><pre><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">在ViewController.m中主要负责两个任务创建子控制器以及TabBarItem</span>

<span style="font-size:18px;">- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//创建子控制器
[self CreateViewControllers];

// 创建tabBarItem
[self createTabbarItem];
}</span>


<span style="font-size:18px;">#pragma mark - 创建子控制器
- (void)CreateViewControllers {
    
    HomepageViewController *homeVC =[[HomepageViewController alloc] init];
    ClassViewController *classifyVC = [[ClassViewController alloc] init];
    SearchViewController *shoppingBagsVC = [[SearchViewController alloc] init];
    PrivateViewController *privateVC = [[PrivateViewController alloc] init];
    NSArray *array = @[homeVC, classifyVC, shoppingBagsVC, privateVC];
    
    NSMutableArray *arrCtrl = [[NSMutableArray alloc] initWithCapacity:4];
    for (int i = 0; i < 4; i++) {
        
        BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:array[i]];
        [arrCtrl addObject:nav];
    }
    self.viewControllers = arrCtrl;
    
}</span>
只要四个头文件都添加了基本没什么问题


接下来是创建自己的TabBar标签

第一步应该是移除系统原有的TabBar

第二步定义每个Tabbar Item的坐标,宽高

第三步,给每个Tabbar Item加一个点击事件

第四步,给每个TabBar Item设置未选中时的图片和选中时的图片


在UITabBarController里如何切换子控制器,取决于self.selectedIndex的赋值,等于0则显示第一个子控制器的内容,等于2则显示第三个子控制器的内容,所以TabBaritem的点击事件就是点击第一个,self.selectedIndex的值就是0,点击第三个self.selectedIndex的值是2


<span style="font-size:14px;">- (void)indexWithButton:(ItemView *)sender {
    //取出tag值
    if (lastItemView != sender) {
        //  NSLog(@"%@",sender.tag);
        [lastItemView setItemSelected:NO];
        [sender setItemSelected:YES];
        lastItemView = sender;
        self.selectedIndex = sender.tag;
    }
}
-(void)createTabbarItem{
    for (UIView *subview in self.tabBar.subviews) {
        [subview removeFromSuperview];
    }//移除apple原有的TabBar——Item
    
    CGFloat itemWidth=kScreenWidth/self.viewControllers.count;
    NSArray *buttonName = @[@"首页", @"分类", @"搜索", @"我的"];
    NSArray *selectName = @[@"home", @"category", @"packege",@"percenter"];
    NSArray *ImgName = @[@"home_a", @"category_a", @"packege_a", @"percenter_a"];
    for (int i = 0; i < 4; i++) {
        
        ItemView *itemView = [[ItemView alloc] initWithFrame:CGRectMake(itemWidth * i, 0, itemWidth, kTabBarHeight)];
        [itemView addTarget:self action:@selector(indexWithButton:) forControlEvents:UIControlEventTouchUpInside];
        itemView.tag =i;
        [itemView setItemImage:[UIImage imageNamed:ImgName[i]] forState:UIControlStateNormal];
        [itemView setItemImage:[UIImage imageNamed:selectName[i]] forState:UIControlStateSelected];
        [itemView setItemTitle:buttonName[i]];
        [self.tabBar addSubview:itemView];
        if (i == 0) {
            [itemView setItemSelected:YES];
            lastItemView = itemView;
        }
    }
    
}
</span>


firstViewController和ViewController其实都并不能难,难得是四个控制器,

下一章介绍主页的控制器HomepageViewController http://blog.csdn.net/u012723810/article/details/50484559



你可能感兴趣的:(iOS(四)动漫App:二)