iOS代码优化 [英] iOS code optimization

查看:46
本文介绍了iOS代码优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 iOS 开发新手,这可能是一个非常基本的问题 - 在我的应用程序中,我有 5 个 VC,每个 VC 上都有 UITabBarController.我正在处理标签栏项目点击-

I'm new in iOS development and this may be a very basic question- in my app i've 5 VC with UITabBarController on each VC. I'm handling tab bar item click by-

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    switch (item.tag)
    {
        case 0:
        {
            vc1 = [[VC1 alloc] initWithNibName:@"VC1" bundle:nil];
            [self.view addSubview:vc1.view];
            [tabbarObj setSelectedItem:[tabbarObj.items objectAtIndex:0]];
        }
            break;
        case 1:
        {
            vc2 = [[VC2 alloc] initWithNibName:@"VC2" bundle:nil];
            [self.view addSubview:vc2];
        }
            break;
        case 2:
        {
            vc3 = [[VC3 alloc] initWithNibName:@"VC3" bundle:nil];
            [self.view addSubview:vc3];
        }
            break;
        case 3:
        {
            [tabbarObj setSelectedItem:[tabbarObj.items objectAtIndex:3]];
        }
            break;
        case 4:
        {
            vc5 = [[VC5 alloc] initWithNibName:@"VC5" bundle:nil];
            [self.view addSubview:vc5];
        }
            break;
        default:
            break;
    }
}

而且我知道这不是处理选项卡栏的正确方法,因为每次我单击选项卡 bat 项目时,它都会在当前视图上添加一个子视图.有人可以建议我更好的方法吗?谢谢.

and i know this is not a right way to handle tab bar because every time when i click on a tab bat item it will add a subView on current view. Can anybody suggest me a better way?Thanks.

推荐答案

在您的 Appedelegate.m 文件中 在 didFinishLaunchingWithOptions 方法中进行更改

in Your Appedelegate.m file Make changes in didFinishLaunchingWithOptions Method

 tabBar_Controller = [[UITabBarController alloc] init];
    NSMutableArray *localControllersArray =[[NSMutableArray alloc]initWithCapacity:2];



    firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];

    nav = [[UINavigationController alloc] initWithRootViewController:firstViewController];
nav.tabBarItem.title = @"item1";
    nav.navigationBar.barStyle = UIBarStyleBlack;
   [localControllersArray addObject:nav];
            [self setNav:nil];

    secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    nav = [[UINavigationController alloc] initWithRootViewController:secondViewController];
    nav.tabBarItem.title = @"item2";
           [localControllersArray addObject:nav];

    [self setNav:nil];
 tabBar_Controller.viewControllers = localControllersArray;
   tabBar_Controller.delegate = self;
    tabBar_Controller.selectedIndex = 0;
[self.window addSubview:tabBar_Controller.view];

要在选择 ViewController 后访问某些属性,请使用以下代码.

To Access Some properties after Your ViewController is Selected Use the following Code.

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewControllers
{
    NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewControllers];

    switch (index) {


        case 0:
        {

            NSLog(@"selected 1");

            break;
        }
        case 1:

          {
            NSLog(@"selected 2");
                    break;
        }
        default:
            break;
    }


}

要更改标签栏控制器的背景图像,您可以使用以下代码.

For Changing Background image of tabbar Controller you can use Following Code.

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewControllers{
    NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewControllers];

    switch (index) {
        case 0:
            [tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_act21.png"]];

            break;
        case 1:
            [tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_act22.png"]];
            break;
        case 2:
            [tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_act23.png"]];
            break;
        default:
            break;
    }

    return YES;
}

这篇关于iOS代码优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆