App主流框架UINavigationController && UITabBarController的简单使用

来源:互联网 发布:php字符串截取 编辑:程序博客网 时间:2024/06/01 20:31

http://blog.csdn.net/yang198907/article/details/49807011


 一个iOS app几乎没有由一个控制器组成,除非这个app非常简单。
      当app中有多个控制器的时候,就需要对这些控制器进行管理,1个控制器去管理其他多个控制器;
      如图所示:
       
 
  • 苹果公司为我提供了两个特殊的控制器,UINavigationController和UITabBarController去管理其它控制器;

    一. UIViewController的简单使用
          利用UINavigationController,可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型例子就是系统自带的“设置”应用;
            


 (1)UINavigationController的view结构
            
(2)UINavigationController工作原理     
            UINavigationController以栈的形式保存子控制器
     如图:
      

  • 使用push方法能将某个控制器压入栈
     - (void)pushViewController:(UIViewController*)viewController animated:(BOOL)animated;

  • 使用pop方法可以移除栈顶控制器
          三种移除方式:
                将栈顶的控制器移除
     - (UIViewController*)popViewControllerAnimated:(BOOL)animated;
                回到指定的子控制器
     - (NSArray*)popToViewController:(UIViewController*)viewController animated:(BOOL)animated;
                回到根控制器(栈底控制器)
     - (NSArray*)popToRootViewControllerAnimated:(BOOL)animated;

导航栏的内容
  • 导航栏的内容由栈顶控制器的navigationItem属性决定
  • UINavigationItem有以下属性影响着导航栏的内容

          左上角的返回按钮
     @property(nonatomic,retain)UIBarButtonItem*backBarButtonItem;

          中间的标题视图
     @property(nonatomic,retain)UIView          *titleView;

          中间的标题文字
     @property(nonatomic,copy)  NSString        *title;
         
          左上角的按钮
     @property(nonatomic,retain)UIBarButtonItem*leftBarButtonItem;

          右上角的按钮
     @property(nonatomic,retain)UIBarButtonItem*rightBarButtonItem;


UINavigationController的使用步骤

     初始化UINavigationController
     设置UIWindowrootViewControllerUINavigationController
     将第一个视图控制器设置为UINavigationController的根视图控制器     
     通过push方法新建子控制器
     通过pop方法返回到上一级控制器

eg:

[objc] view plain copy
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  2.   // Override point for customization after application launch.  
  3.   //实例化window  
  4.   self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];  
  5.   //创建UINavigationController  
  6.   UINavigationController *nvc = [[UINavigationController alloc] init];  
  7.   
  8.   //创建两个UIViewController,并设置背景色,push cv1,两秒后push vc2  
  9.   UIViewController *cv1 = [[UIViewController alloc] init];  
  10.   cv1.view.backgroundColor = [UIColor redColor];  
  11.   
  12.   UIViewController *cv2 = [[UIViewController alloc] init];  
  13.   cv2.view.backgroundColor = [UIColor greenColor];  
  14.   [nvc pushViewController:cv1 animated:YES];  
  15.   
  16.   dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(22 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{  
  17.       [nvc pushViewController:cv2 animated:YES];  
  18.   });  
  19.   
  20.   //将UINavigationController设为window的根控制器  
  21.   self.window.rootViewController = nvc;  
  22.   [self.window makeKeyAndVisible];  
  23.   return YES;  

//UINavigationController的另一种初始化方法
- (instancetype)initWithRootViewController:(UIViewController*)rootViewController;// 

二.UITabBarController的简单使用
     跟UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型例子就是QQ、微信等应用。
      
1.UITabBarController的view结构
     

2.UITabBarController的工作原理
     如果UITabBarControllerN个子控制器,那么UITabBar内部就会有NUITabBarButton作为子控件
     例如UITabBarController3个子控制器,UITabBar的结构大致如下:

      

   UITabBarButton里面显示什么内容,由对应子控制器的tabBarItem属性决定
   UITabBarItem有以下属性影响着UITabBarButton的内容
        
     标题文字
    @property(nonatomic,copy)NSString*title;
     图标
    @property(nonatomic,retain)UIImage*image;
     选中时的图标
    @property(nonatomic,retain)UIImage*selectedImage;
     提醒数字
    @property(nonatomic,copy)NSString*badgeValue

 添加单个子控制器
     - (void)addChildViewController:(UIViewController*)childController;
  
       UITabBarController的使用步骤
  1. 初始化UITabBarController
  2. 设置UIWindowrootViewControllerUITabBarController
  3. 根据具体情况,通过addChildViewController方法添加对应个数的子控制器
     
     eg:
[objc] view plain copy
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  2.     // Override point for customization after application launch.  
  3.     self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];  
  4.   
  5.     UITabBarController *tabVc = [[UITabBarController alloc] init];  
  6.   
  7.     UIViewController *vcOne = [[UIViewController alloc] init];  
  8.     vcOne.view.backgroundColor = [UIColor redColor];  
  9.     vcOne.tabBarItem.title = @"one";  
  10.     vcOne.tabBarItem.image = [UIImage imageNamed:@"icon1"];  
  11.     [tabVc addChildViewController:vcOne];  
  12.   
  13.     UIViewController *vcTwo = [[UIViewController alloc] init];  
  14.     vcTwo.view.backgroundColor = [UIColor blueColor];  
  15.     vcTwo.tabBarItem.title = @"two";  
  16.     vcTwo.tabBarItem.image = [UIImage imageNamed:@"icon2"];  
  17.     [tabVc addChildViewController:vcTwo];  
  18.   
  19.     self.window.rootViewController = tabVc;  
  20.     [self.window makeKeyAndVisible];  
  21.     return YES;  
  22. }  


运行效果:
     
三.目前主流的App框架
          如:qq,微信,UITabBarController中嵌套UINavigationController
              

          如:易信等 UINavigationController中嵌套UITabBarController
             


以上,并非严格意义上上的嵌套,TabBarController和NavigationController组合使用,能达到非常不错的效果。


四.Segue
     以上TabBarController和NavigationController的简单使用,都是干掉main.storyboard后用代码的方式来创建;
当使用main.storyboard时,就会涉及到segue。
     什么是segue呢?
      Storyboard上每一根用来界面跳转的线,都是一个UIStoryboardSegue对象(简称Segue
          如图所示:
           
1.Segue的属性
          唯一标识
     @property(nonatomic,readonly)NSString*identifier;
          来源控制
     @property(nonatomic,readonly)idsourceViewController;
          目标控制器
     @property(nonatomic,readonly)iddestinationViewController;
            如图所示:
           
          

2.Segue的类型
     根据Segue的执行(跳转)时刻,Segue可以分为2大类型
  1. 自动型:点击某个控件后(比如按钮),自动执行Segue,自动完成界面跳转
  2. 手动型:需要通过写代码手动执行Segue,才能完成界面跳转
          拖线方式:
          按住Control键,直接从控件拖线到目标控制器,此时为“自动型Segue”
  • 按住Control键,从来源控制器拖线到目标控制器此时为“手动型Segue”,此时为了方便使用,需要设置Segue的identifier属性。
  • 使用“手动型Segue”时:在需要的时刻,由来源控制器执行perform方法调用对应的Segue
  • [self performSegueWithIdentifier:identifiersender:nil];
               完整过程是:self是来源控制器
                                     根据identifierstoryboard中找到对应的线,新建UIStoryboardSegue对象
                                     设置Segue对象的sourceViewController(来源控制器)          
                                     新建并且设置Segue对象的destinationViewController(目标控制器)

          我们可以根据需求:如果点击某个控件,不需要做任何判断,直接跳转到下一个界面,则使用“自动型Segue”,反之则用用“手动型Segue”;

  3.常用的方法
     performSegueWithIdentifier:sender
     调用sourceViewController的下面方法,做跳转前的准备工作并传入创建好的Segue对象
  - (void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender;
     sender调用performSegueWithIdentifier:sender:方法时传入的对象

  • 调用Segue对象的- (void)perform;方法开始执行界面跳转操作
  • 如果seguestylepush
  • 取得sourceViewController所在的UINavigationController
  • 调用UINavigationControllerpush方法将destinationViewController压入栈中,完成跳转
  • 如果seguestylemodal
  • 调用sourceViewControllerpresentViewController方法将destinationViewController展示出来
4.Modal
  • 除了push之外,还有另外一种控制器的切换方式,那就是Modal
  • 任何控制器都能通过Modal的形式展示出来
  • Modal的默认效果:新控制器从屏幕的最底部往上钻,直到盖住之前的控制器为止
  • Modal的形式展示控制器

- (void)presentViewController:(UIViewController*)viewControllerToPresent animated: (BOOL)flag completion:(void(^)(void))completion

  • 关闭当初Modal出来的控制器

- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void(^)(void))completion;

  • 原则:谁Modal,谁dismiss

5.控制器的数据传递
     控制器的数据传递(顺传)
  • 控制器的跳转方向:  A ->B
  • 数据的传递方向:      A -> B
  • 数据的传递方式 AprepareForSegue:sender:方法中根据segue参数取得destinationViewController,也就是控制器B直接给控制器B传递数据
  • BviewDidLoad方法中取得数据或者利用setter方法设置界面上的UI控件;
          eg: 将当前的控制器中的self.modelArray[indexPath.row]数据传递给EditViewController:
[objc] view plain copy
  1. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{  
  2.         EditViewController *editVc = (EditViewController *)segue.destinationViewController;  
  3.         NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];  
  4.         editVc.model = self.modelArray[indexPath.row];  
  5.      }  

    需要重写EditViewController 的属性model的setter;然后在viewDidLoad的设置EditViewController内部其它属性赋值。(需要谨防viewDidLoad还没有加载完成,就对内部的控件赋值,否则会bug);
    @property (nonatomic,strong)Model *model;

         控制器的数据传递(逆传
  • 控制器的跳转方向  A-> B
  • 数据的传递方向:      B -> A
  • 数据的传递方式: A成为B的代理,在B中调用A的代理方法,通过代理方法的参数传递数据给A

阅读全文
0 0
原创粉丝点击