UITabBar(第二次启动程序默认界面是上一次退出时的界面)

创建第一个视图控制器对象

    FirestViewController *firstVC = [[FirestViewControlleralloc]init];

    

创建一个navc

    UINavigationController *firstNAVC = [[UINavigationControlleralloc]initWithRootViewController:firstVC];

    

创建tabbar上的按钮及其内容

    firstVC.tabBarItem = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemHistorytag:1000];

    

创建第二个

    SecondViewController  *secVC = [[SecondViewControlleralloc]init];

    UINavigationController *secNAVC = [[UINavigationControlleralloc]initWithRootViewController:secVC];

    secVC.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"朋友圈"image:[UIImageimageNamed:@"1.png"]selectedImage:[UIImageimageNamed:@"2.png"]];

    

创建第三个

    ThirdViewController *tihVC = [[ThirdViewControlleralloc]init];

    UINavigationController *tihNAVC = [[UINavigationControlleralloc]initWithRootViewController:tihVC];

    tihVC.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"设置"image:[UIImageimageNamed:@"3.png"]tag:1001];

    

创建第四个

    FourthViewController *fourVC = [[FourthViewControlleralloc]init];

    UINavigationController *fourNAVC = [[UINavigationControlleralloc]initWithRootViewController:fourVC];

    fourVC.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"推广"image:[UIImageimageNamed:@"4.png"]tag:1002];

    

创建第五个

    FifthViewController *fifthVC = [[FifthViewControlleralloc]init];

    UINavigationController *fifthNAVC = [[UINavigationControlleralloc]initWithRootViewController:fifthVC];

    fifthVC.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"联系人"image:[UIImageimageNamed:@"5.png"]selectedImage:[UIImageimageNamed:@"6.png"]];

    

创建第六个

    SixthViewController *sixVC = [[SixthViewControlleralloc]init];

    UINavigationController *sixNAVC = [[UINavigationControlleralloc]initWithRootViewController:sixVC];

    sixVC.tabBarItem = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemHistorytag:1003];

    

按钮创建好了,然后创建一个UITabbarController让所有的按钮显示出来

    UITabBarController *tabVC = [[UITabBarControlleralloc]init];

    

tabbarconroller通过一个数组来管理所有要显示出来的naVC

    tabVC.viewControllers =@[firstNAVC, secNAVC, tihNAVC, fourNAVC,fifthNAVC,sixNAVC];

    self.window.rootViewController = tabVC;

    firstVC.tabBarItem.badgeValue =@"+99";

    


tabbar进行外观设置

透明度

    tabVC.tabBar.translucent =NO;


颜色

    tabVC.tabBar.barTintColor = [UIColororangeColor];

被点击后的图标颜色

    tabVC.tabBar.tintColor = [UIColorpurpleColor];



第二次启动程序默认界面是上一次退出时的界面

1.首先拿到一个值 通过NSUserDefaults key 值必须跟取值一致 如果有值的话就遍历一下Controllers   

    NSString *viewControllerTitle = [[NSUserDefaultsstandardUserDefaults]objectForKey:SELECTEDKEY];

    if (viewControllerTitle) {

       for (UIViewController *vcin tabVC.viewControllers) {

           if ([vc.tabBarItem.titleisEqualToString:viewControllerTitle]) {

                tabVC.selectedViewController = vc;

            }

        }

    }

    

  

点击之后去掉+99 (协议方法)<UITabBarControllerDelegate>

设置代理人

    tabVC.delegate =self;

    

设置默认选择第几个图标

tabVC.selectedIndex = 2;

    



释放

    [tabVCrelease];

    [firstNAVCrelease];

    [firstVCrelease];

    [secNAVCrelease];

    [secVCrelease];

    [tihNAVCrelease];

    [tihVCrelease];

    [fourNAVCrelease];

    [fourVCrelease];

    [fifthVCrelease];

    [fifthNAVCrelease];

    [sixVCrelease];

    [sixNAVCrelease];

      

    return YES;

}


第二次启动程序默认界面是上一次退出时的界面

去掉+99图标

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

{

    viewController.tabBarItem.badgeValue = @"";//点击之后出现红圈

    viewController.tabBarItem.badgeValue =nil;


第二次启动程序默认界面是上一次退出时的界面

   实现:通过方法得到NSUserDefaults,他是指向应用程序的plist文件

    NSUserDefaults *defatults = [NSUserDefaultsstandardUserDefaults];

    

    如果可以拿到视图控制器的标题,调用setobject方法,将标题记录在NSUserDefaults, key定义一个常量取值的时候要与设定的值保持一致

    if (viewController.tabBarItem.title) {

        [defatultssetObject:viewController.tabBarItem.titleforKey:SELECTEDKEY];

        

       将数据同步给应用程序

        [defatultssynchronize];(如果不写存储将无效)

  

       下次进入程序,如果在NSUserDefaults能取到viewController.title则会直接将标题栏控制器直接指向视图控制器,直接是视图控制器所对应的tabBar处于被选中的状态

    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值