UITabBarController

1.创建一个UITableController

2.为每个Tab创建一个视图控制器

3.创建一个array  把视图控制器都添加到array中 在把array放到UITabBarCtrl的Controllers中

4.设置根视图为tabBar

*只有在支持旋转的设置  才会旋转  发生旋转时只有当前的ViewController才会接受到旋转的消息

*对于UITabBar自带的tabBar  不能直接去修改

//修改背景图片

UIImage *tabBackground = [UIImage imageNamed:@"main_title2.png"];

   if ([tabBarController.tabBar respondsToSelector:@selector(setBackgroundImage:)]){

        [tabBarController.tabBar setBackgroundImage:tabBackground];

    }

常用委托

//当点击tabBarItem时触发该操作  

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *) viewController {

    NSLog(@"%d", viewController.tabBarItem.tag);

}

通过代理我们可以监测UITabBarController的当前选中viewController的变化,以及moreViewController中对编辑所有viewController的编辑。通过实现下面方法:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;

  该方法用于控制TabBarItem能不能选中,返回NO,将禁止用户点击某一个TabBarItem被选中。但是程序内部还是可以通过直接setSelectedIndex选中该TabBarItem。

  下面这三个方法主要用于监测对moreViewController中对view controller的edit操作

 

- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers;

 

- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;

 

- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;

 

改变UITabBarController中当前显示的viewController

 1、selectedIndex属性

  通过该属性可以获得当前选中的viewController,设置该属性,可以显示viewControllers中对应的index的viewController。如果当前选中的是MoreViewController的话,该属性获取出来的值是NSNotFound,而且通过该属性也不能设置选中MoreViewController。设置index超出viewControllers的范围,将会被忽略。

  2、selectedViewController属性

  通过该属性可以获取到当前显示的viewController,通过设置该属性可以设置当前选中的viewController,同时更新selectedIndex。可以通过给该属性赋值

tabBarController.moreNavigationController可以选中moreViewController。

  3、viewControllers属性

  设置viewControllers属性也会影响当前选中的viewController,设置该属性时UITabBarController首先会清空所有旧的viewController,然后部署新的viewController,接着尝试重新选中上一次显示的viewController,如果该viewController已经不存在的话,会接着尝试选中index和selectedIndex相同的viewController,如果该index无效的话,则默认选中第一个viewController。

 

 

UITabBarItem

//小红圈提示

barItem1.badgeValue = @"1";

//给选中的状态和未选中的状态指定不同的图片

[item setFinishedSelectedImage:[UIImage imageNamed:@"second.png"] 

   withFinishedUnselectedImage:[UIImage imageNamed:@"first.png"]];

 

//每个UIController中都有一个navigationItem和一个tabBarItem 一般创建一个相应类型的对象  对其赋值

//系统样式

    UITabBarItem * barItem1 = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:0];

    barItem1.badgeValue = @"1";

    yvCtrl.tabBarItem = barItem1;

//自定义样式

firstController.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"First" image:[UIImage imageNamed:@"img.png"] tag:101]

 

//重写ViewController里的init   在初始化里添加Item

(id)init {  

    if ([super init] != nil) {  

        UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"asdfsadf" image:[UIImage imageNamed:@"WWAN5.png"] tag:1];  

        self.tabBarItem = item;  

        [item release];  

    }  

    return self;  



创建一个UITabConroller:


.h文件中:

#import<UIKit/UIKit.h>

@interface AppDelegate :UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
{
    UITabBarController*     tabBarController;
}

@property (strong,nonatomic)UIWindow *window;
@property (nonatomic,retain) UITabBarController*     tabBarController;
-(void) initTabBarController;

@end




.m文件中:

-(void) initTabBarController
{
   // create a tab bar controller
   self.tabBarController = [[UITabBarControlleralloc]init];
    self.tabBarController.delegate =self;
   //1====>navhomeController
    HomeController* homeView = [[HomeControlleralloc]initWithNibName :@"HomeController"bundle : nil];
    UINavigationController* navhomeController = [[UINavigationControlleralloc]initWithRootViewController : homeView];
    [homeView release];
    [navhomeController.navigationBarcustomed];
    [navhomeController.navigationBarcustomedBarBackground];
    navhomeController.navigationBar.topItem.title =@"文件查看";
    navhomeController.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"文件查看" image:[UIImageimageNamed:@"home.png"] tag :0];
    
   TempViewController* temp1 = [[TempViewControlleralloc]initWithNibName:@"TempViewController"bundle:nil];
   UINavigationController* navTempController = [[UINavigationControlleralloc]initWithRootViewController : temp1];
    [temp1 release];
    navTempController.navigationBar.topItem.title =@"最近";
    navTempController.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"最近"image:[UIImageimageNamed:@"icon_bookmark.png"] tag :1];
    
   DesktopViewController* desktopView = [[DesktopViewControlleralloc]initWithNibName:@"DesktopViewController"bundle:nil];
    UINavigationController* navDesktopView = [[UINavigationControlleralloc]initWithRootViewController : desktopView];
    [desktopView release];
    [navDesktopView.navigationBarcustomed];
    navDesktopView.navigationBar.topItem.title =@"桌面";
    navDesktopView.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"桌面"image:[UIImageimageNamed:@"icon_attention.png"] tag :2];
    
   TempViewController* temp3 = [[TempViewControlleralloc]initWithNibName:@"TempViewController"bundle:nil];
   UINavigationController* navTempController3 = [[UINavigationControlleralloc]initWithRootViewController : temp3];
    [temp3 release];
    navTempController3.navigationBar.topItem.title =@"搜索";
    navTempController3.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"搜索"image:[UIImageimageNamed:@"icon_search.png"] tag :3];
    
    
   //5====>SettingController
    SettingController* settingView = [[SettingControlleralloc]initWithNibName :@"SettingController"bundle : nil];
    UINavigationController* navSettingView = [[UINavigationControlleralloc]initWithRootViewController : settingView];
    [settingView release];
    navSettingView.navigationBar.topItem.title =@"设置";
    navSettingView.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"设置"image:[UIImageimageNamed:@"icon_setting.png"] tag :4];
    
    tabBarController.viewControllers = [NSArrayarrayWithObjects : navhomeController,navTempController,navDesktopView,navTempController3,navSettingView,nil];
    
    OCRELEASE(navhomeController);
    OCRELEASE(navTempController);
    OCRELEASE(navDesktopView);
    OCRELEASE(navTempController3);
    OCRELEASE(navSettingView);
    
   // add navigation controllers to the tab bar controller
   //    tabBarController.viewControllers = [NSArray arrayWithObjects : navhomeController,navPersonalController,navAttentionView,navSeekView,navSettingView,nil];
   //    OCRELEASE(navhomeController);
   //    OCRELEASE(navPersonalController);
   //    OCRELEASE(navAttentionView);
   //    OCRELEASE(navSeekView);
   //    OCRELEASE(navSettingView);
}
@synthesize tabBarController;

- (void)dealloc
{
    [_windowrelease];
    [tabBarControllerrelease];
    [superdealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   self.window = [[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];
   // Override point for customization after application launch.
    [selfinitTabBarController];
    
   self.window.rootViewController =tabBarController;
    [self.windowmakeKeyAndVisible];
   returnYES;
}




  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值