UINavigationController与UITabBarController的结合

到目前为止,iphone app的设计界面模式大概就三种:
1。上有导航栏、下有tab选项的app(此类app最多)
2。只有上面有导航栏的app(较少)
3。只有下面有tab选项的app(非常少,可能是无法展示太多的内容吧,个人觉得)

其中着重讲解一下第一种设计方式:
  UIViewController,就那个界面框架,里面即含有navigation也含有tabbar
  在创建此类app的时候,不要采取xcode中的UINavigation项目或是UITabBar类型的项目,直接创建view-base类的项目,然后在delegete的.m文件中添加----
     - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

        CustomTabBarControler *tabbar =  [[CustomTabBarControler alloc] init];//CustomTabBarControler是我们自己重新定义的UITabBarController类
        self.tabbarController = tabbar;
        [tabbar release];
       
        // home
        HomeViewControler *homeViewControler = [[HomeViewControler alloc] init];
        HomeNAVControler *homeNavControler = [[HomeNAVControler alloc] initWithRootViewController:homeViewControler];
        [homeViewControler release];
       
       
        homeNavControler.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"Home" image:[UIImage imageNamed:@"Btn_Home.png"] tag:0];
       
        homeNavControler.navigationItem.title=@"Home";
        homeNavControler.navigationBar.hidden=YES;
       
        // search
        UIViewController *searchViewControler;
        //if([[UserService shareInstance] isUserLogin]){
        // searchViewControler = [[OrderListViewController alloc] init];
        //}
        //else
        searchViewControler = [[TrackOrderViewController alloc] init]; 
       
        UINavigationController *searchNavControler = [[UINavigationController alloc] initWithRootViewController:searchViewControler];
        [searchViewControler release];
       
        searchNavControler.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"Track Order" image:[UIImage imageNamed:@"Btn_Track.png"] tag:1];   
        searchNavControler.navigationItem.title=@"Track Order";
        searchNavControler.navigationBar.hidden=NO;
       
        // cart
        CartViewControler *cartViewControler = [[CartViewControler alloc] init];
        UINavigationController *cartNavControler = [[UINavigationController alloc] initWithRootViewController:cartViewControler];
        [cartViewControler release];
       
        cartNavControler.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"Cart" image:[UIImage imageNamed:@"Btn_Cart.png"] tag:2];  
        //cartNavControler.tabBarItem.badgeValue=@"0";
        cartNavControler.navigationItem.title=@"Cart";
        cartNavControler.navigationBar.hidden=NO;
       
       
        // wishlist
        WishListViewControler *wishlistViewControler = [[WishListViewControler alloc] init];
        UINavigationController *wishlistNavControler = [[UINavigationController alloc] initWithRootViewController:wishlistViewControler];  
        [wishlistViewControler release];
       
        wishlistNavControler.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"Wish List" image:[UIImage imageNamed:@"Btn_Wish.png"] tag:3];  
        //wishlistNavControler.tabBarItem.badgeValue=@"0";
        wishlistNavControler.navigationItem.title=@"Wish List";
        wishlistNavControler.navigationBar.hidden=NO;
       
        // more   
        MoreViewControler *moreViewControler = [[MoreViewControler alloc] init];
        UINavigationController *moreNavControler = [[UINavigationController alloc] initWithRootViewController:moreViewControler];
        [moreViewControler release];
       
        moreNavControler.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"More" image:[UIImage imageNamed:@"Btn_More.png"] tag:4];  
        moreNavControler.navigationItem.title=@"More";
        moreNavControler.navigationBar.hidden=NO;
       
        // add navControler to tabbar  一tabcontroler为主,再往其中添加navcontroller
        self.tabbarController.viewControllers = [NSArray arrayWithObjects:homeNavControler,searchNavControler,cartNavControler,wishlistNavControler,moreNavControler, nil]; 
       
        [self.window addSubview:self.tabbarController.view];
       
        [homeNavControler release];
        [searchNavControler release];
        [cartNavControler release];
        [wishlistNavControler release];
        [moreNavControler release];
      
    [self.window makeKeyAndVisible];
   
    return YES;
}


//
//  CustomTabBarControler.h
//  Tmart
//
//  Created by zongteng on 11-7-20.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "ItemImageViewControler.h"

@interface CustomTabBarControler : UITabBarController<UITabBarControllerDelegate> {
   
}

@end



//
//  CustomTabBarControler.m
//  Tmart
//
//  Created by zongteng on 11-7-20.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "CustomTabBarControler.h"


@implementation CustomTabBarControler

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    UIViewController *viewController = self.selectedViewController;
    if([viewController isKindOfClass:[UINavigationController class]]){
        viewController = [(UINavigationController *)viewController visibleViewController];
    }
    return [viewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}

//??
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    UINavigationController *vv = (UINavigationController *)viewController;
    [vv popToRootViewControllerAnimated:YES];
}
@end

原文地址:https://www.cnblogs.com/cnsec/p/11515900.html