分享

导航栏与标签栏

 叹落花 2014-12-20

1.直接在根视图控制器上显示标签栏和导航栏

只有一个根视图控制器,在AppDelegate.m文件中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)lanuchOptions方法中添加如下代码:

  1. //创建导航栏对象  
  2. UINavigationController *pNavigation = [[UINavigationController alloc]initWithRootViewController:self.viewController];  
  3. //创建另外一个类的对象  
  4. LinAnotherViewController * pAnotherVC = [[LinAnotherViewController alloc]initWithNibName:nil bundle:nil];  
  5. //创建Tabbar对象  
  6. UITabBarController *pTabBar = [[UITabBarController alloc]init];  
  7. //初始化数组,存储标签栏的内容  
  8. NSArray *pArray = [NSArray arrayWithObjects:pNavigation,pAnotherVC, nil nil];  
  9. //把数组中内容传递给标签栏控制器  
  10. pTabBar.viewControllers = pArray;  
  11. //设置根视图控制器  
  12. self.window.rootViewController = pTabBar;  

此时视图都为空,可按照之前学习的方法添加相应的控件、图片、设置导航栏、标签栏的属性等。

2.新建根视图控制器,显示切换界面后的标签栏和导航栏

当需要在页面跳转后显示标签栏和导航栏,此时应该设置新的根视图,在它上面编写创建相应的视图控制器、导航栏,再添加到标签栏控制器中。需要实现AppDelegate的委托创建新的根视图。
首先是导入新视图的名称,特别是:LinAppDelegate.h文件
  1. #import "LinFirstViewController.h"  
  2. #import "LinSecondViewController.h"  
  3. #import "LinThirdViewController.h"  
  4. //委托协议代理  
  5. #import "LinAppDelegate.h"  
重写- (void)viewDidLoad方法:
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     //创建视图对象和相应的导航栏对象,假设均有导航栏  
  5.     LinFirstViewController * pFirstVC = [[LinFirstViewController alloc]initWithNibName:nil bundle:nil];  
  6.     UINavigationController * pFirstNavigation = [[UINavigationController alloc]initWithRootViewController:pFirstVC];  
  7.     LinSecondViewController * pSecondVC = [[LinSecondViewController alloc]initWithNibName:nil bundle:nil];  
  8.     UINavigationController * pSecondNavigation = [[UINavigationController alloc]initWithRootViewController:pSecondVC];  
  9.     LinThirdViewController * pThirdVC = [[LinThirdViewController alloc]initWithNibName:nil bundle:nil];  
  10.     UINavigationController * pThirdNavigation = [[UINavigationController alloc]initWithRootViewController:pThirdVC];  
  11.     //初始化数组,存储导航控制器  
  12.     NSArray * array = [[NSArray alloc]initWithObjects:pFirstNavigation, pSecondNavigation, pThirdNavigation, nil nil];  
  13.     //初始化标签栏控制器  
  14.     UITabBarController * pTabBar = [[UITabBarController alloc]init];  
  15.     //设置标签栏中视图控制器(数组)  
  16.     pTabBar.viewControllers = array;  
  17.     //根据委托协议调用方法  
  18.     [self goInNemView:pTabBar];  
  19. }  
  20. //构造委托协议的方法,把标签控制器放在新的根视图中  
  21. - (void)goInNemView:(id)sender  
  22. {  
  23.     //获取当前程序  
  24.     UIApplication * app = [UIApplication sharedApplication];  
  25.     //创建应用程序的委托对象  
  26.     LinAppDelegate * pDelegate = app.delegate;  
  27.     //设置新的根视图控制器,用委托的方法实现代理  
  28.     pDelegate.window.rootViewController = sender;  
  29. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多