zoukankan      html  css  js  c++  java
  • [课堂实践与项目]NavigationController与TabBarController的综合使用及易错点分析(包含消息提醒,app更新)

    陈述:我们在使用tabbarController的时候,我们总会和NavagationController联合起来。但是不联合的时候又是什么样的一种pool的情况呢?我们就单单的 TabBarController  来进行介绍。最后复习一下两个空间结合起来达成的完美效果

    、、-----------------单独的TabbarController的使用

    1.我们先使用TabbarController 来进行view的创建


    由于h文件中没什么内容,我们就直接跳转到代理的实现文件吧

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.window.backgroundColor = [UIColor whiteColor];
        
        LCFirstViewController *firstVC = [[LCFirstViewController alloc]init];
        LCSecondViewController *secondVC = [[LCSecondViewController alloc]init];
        
    
         NSArray *array = [NSArray arrayWithObjects:firstVC,secondVC, nil];
        
        UITabBarController *tabBarVC = [[UITabBarController alloc]init];
        tabBarVC.viewControllers = array;
        [self.window setRootViewController:tabBarVC];
        
        [self.window makeKeyAndVisible];
        return YES;
    }

    可以看得出,我们把两个VC文件封装成了一个 Array的组,然后放到 UITabBarController的viewControllers下。这样子来说,我们的两个视图就做好了,我们运行之后,可能会发现我们的APP 竟然是空白一片。好了,我忘记说了,我们可以设置一些标志。button什么的都是temp的。

    那么我们如何设置我们的item名字为 一个有意义的名字呢 ?

    2.看看我们的firstViewController的init,你就明白了

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
    
         // self.title = @"first";
            self.tabBarItem.title = @"firstView";
          self.tabBarItem.image = [UIImage imageNamed:@"1.png"];
            
    
        }
        return self;
    }


    你可能会惊讶,我们的h m文件中都没有 tabbarItem,但是为什么self之后就能出现呢?

    因为这是apple因为为我们封装好的框架,但是我们没有使用的时候,是hidden的。有的时候,必须设置成 no才可以的。

    如果你把我注释的  self.title = @“first”去掉注释,然后去掉 下面的那句,那你就会发现, 我们的item的名称就会变化。这是为什么么?self.title有什么牛逼的作用?我们在导航介绍完之后就会告诉大家。别着急哦


    //导航控制器和 TabBarController的综合使用

    3.现在我们就修改一下,delegate.m中的一些代码,来实现综合使用吧

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.window.backgroundColor = [UIColor whiteColor];
        
        LCFirstViewController *firstVC = [[LCFirstViewController alloc]init];
        LCSecondViewController *secondVC = [[LCSecondViewController alloc]init];
        
        
        UINavigationController *navFirstVC = [[UINavigationController alloc]initWithRootViewController:firstVC];
       UINavigationController *navSecondVC = [[UINavigationController alloc]initWithRootViewController:secondVC];
     
      NSArray *array = [NSArray arrayWithObjects:navFirstVC,navSecondVC, nil];
      
        
        UITabBarController *tabBarVC = [[UITabBarController alloc]init];
        tabBarVC.viewControllers = array;
        [self.window setRootViewController:tabBarVC];
        
        [self.window makeKeyAndVisible];
        return YES;
    }


    其实很简单,我们只是修改了 我们的数组里面的 vc的属性,从原来的 vc到现在的nvc(NavigationVC)。这一下子,我们就让一个vc有了导航栏的属性。

    来,看看效果吧



    4.看完之后,你也许就会问,1.2的差别好大呢,差别到底在哪里呢?

    我们来看看 1.2m的init方法吧,对比才有好坏~

    1.m

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
    
          self.title = @"first";
            self.tabBarItem.title = @"firstView";
          self.tabBarItem.image = [UIImage imageNamed:@"1.png"];
            
    
        }
        return self;
    }


    2.m

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            
            self.tabBarItem.title = @"secondView";
            // Custom initialization
        }
        return self;
    }

    看到2的时候,你觉得 为什么设置了title,却没有显示呢 ?

    就此问题,我试验了四次,终于明白了

    1)只设置一项,是不出现的。上述代码已验证

    2)设置导航的title,只出现了导航的title。

    3).设置成 self.title,都出现。



    4).tabbarItem.title和image一同设置,全部出现。



    5).如果我们设置了self.title,再次设置self.tabbaritem.title,就会发现两者可以不一样了。



    3.现在就我觉得需要声明的几点总结一下:

    i:你写代码加入image的时候,你会发现我们的image都是像图示的 蓝色方框,并不是我们需要的图片,这是因为tabbarItem需要的是一个没有背景的图片,而我们加入的却是有背景的。

    ii:一般情况下,不要仅仅使用self.tabBarItem.title=  @“XXX”,因为我们会发现这行代码是不起作用的 ,要不我加入 image,要不我们再加入 self.title,这样子我们设置的显示才能成功。

    iii:关于提示消息的提示、更新提醒.


    IV:应用更新提醒



  • 相关阅读:
    艾伟_转载:基于.NET平台的Windows编程实战(三)—— 项目的创建及主界面的设计 狼人:
    艾伟_转载:C# Design Patterns (2) Strategy 狼人:
    艾伟_转载:C# Design Patterns (5) Prototype 狼人:
    艾伟_转载:正则表达式30分钟入门教程 狼人:
    艾伟_转载:WCF安全之EndPointIdentity 狼人:
    艾伟_转载:老赵谈IL(3):IL可以看到的东西,其实大都也可以用C#来发现 狼人:
    艾伟_转载:.NET平台上的Memcached客户端介绍 狼人:
    艾伟_转载:关于.NET中的循环引用 狼人:
    艾伟_转载:VS 2008快捷键 狼人:
    艾伟_转载:Regex.Replace 方法的性能! 狼人:
  • 原文地址:https://www.cnblogs.com/riasky/p/3471284.html
Copyright © 2011-2022 走看看