首页 技术 正文
技术 2022年11月7日
0 收藏 984 点赞 1,132 浏览 6991 个字

 一个iOS app几乎没有由一个控制器组成,除非这个app非常简单。      当app中有多个控制器的时候,就需要对这些控制器进行管理,用1个控制器去管理其他多个控制器;      如图所示:       

  • 苹果公司为我提供了两个特殊的控制器,UINavigationController和UITabBarController去管理其它控制器;

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

 (1)UINavigationController的view结构
            iOS -App主流框架UINavigationController && UITabBarController的简单使用
(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     设置UIWindow的rootViewController为UINavigationController     将第一个视图控制器设置为UINavigationController的根视图控制器          通过push方法新建子控制器     通过pop方法返回到上一级控制器

eg:

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//实例化window
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//创建UINavigationController
UINavigationController *nvc = [[UINavigationController alloc] init]; //创建两个UIViewController,并设置背景色,push cv1,两秒后push vc2
UIViewController *cv1 = [[UIViewController alloc] init];
cv1.view.backgroundColor = [UIColor redColor]; UIViewController *cv2 = [[UIViewController alloc] init];
cv2.view.backgroundColor = [UIColor greenColor];
[nvc pushViewController:cv1 animated:YES]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[nvc pushViewController:cv2 animated:YES];
}); //将UINavigationController设为window的根控制器
self.window.rootViewController = nvc;
[self.window makeKeyAndVisible];
return YES;
}

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

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

iOS -App主流框架UINavigationController && UITabBarController的简单使用
1.UITabBarController的view结构
     iOS -App主流框架UINavigationController && UITabBarController的简单使用
2.UITabBarController的工作原理     如果UITabBarController有N个子控制器,那么UITabBar内部就会有N个UITabBarButton作为子控件     例如UITabBarController有3个子控制器,UITabBar的结构大致如下:

      

   UITabBarButton里面显示什么内容,由对应子控制器的tabBarItem属性决定   UITabBarItem有以下属性影响着UITabBarButton的内容        iOS -App主流框架UINavigationController && UITabBarController的简单使用
     标题文字    @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. 设置UIWindow的rootViewController为UITabBarController
  3. 根据具体情况,通过addChildViewController方法添加对应个数的子控制器

          eg:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; UITabBarController *tabVc = [[UITabBarController alloc] init]; UIViewController *vcOne = [[UIViewController alloc] init];
vcOne.view.backgroundColor = [UIColor redColor];
vcOne.tabBarItem.title = @"one";
vcOne.tabBarItem.image = [UIImage imageNamed:@"icon1"];
[tabVc addChildViewController:vcOne]; UIViewController *vcTwo = [[UIViewController alloc] init];
vcTwo.view.backgroundColor = [UIColor blueColor];
vcTwo.tabBarItem.title = @"two";
vcTwo.tabBarItem.image = [UIImage imageNamed:@"icon2"];
[tabVc addChildViewController:vcTwo]; self.window.rootViewController = tabVc;
[self.window makeKeyAndVisible];
return YES;
}

运行效果:
     iOS -App主流框架UINavigationController && UITabBarController的简单使用三.目前主流的App框架          如:qq,微信,UITabBarController中嵌套UINavigationController              iOS -App主流框架UINavigationController && UITabBarController的简单使用
          如:易信等
UINavigationController中嵌套UITabBarController             

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

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

iOS -App主流框架UINavigationController && UITabBarController的简单使用          iOS -App主流框架UINavigationController && UITabBarController的简单使用
2.Segue的类型     根据Segue的执行(跳转)时刻,Segue可以分为2大类型

  1. 自动型:点击某个控件后(比如按钮),自动执行Segue,自动完成界面跳转
  2. 手动型:需要通过写代码手动执行Segue,才能完成界面跳转

          拖线方式:          按住Control键,直接从控件拖线到目标控制器,此时为“自动型Segue”

  • 按住Control键,从来源控制器拖线到目标控制器此时为“手动型Segue”,此时为了方便使用,需要设置Segue的identifier属性。
  • 使用“手动型Segue”时:在需要的时刻,由来源控制器执行perform方法调用对应的Segue
  • [self
    performSegueWithIdentifier:identifiersender:nil];

               完整过程是:self是来源控制器                                     根据identifier去storyboard中找到对应的线,新建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;方法开始执行界面跳转操作
  • 如果segue的style是push
  • 取得sourceViewController所在的UINavigationController
  • 调用UINavigationController的push方法将destinationViewController压入栈中,完成跳转
  • 如果segue的style是modal
  • 调用sourceViewController的presentViewController方法将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
  • 数据的传递方式: 在A的prepareForSegue:sender:方法中根据segue参数取得destinationViewController,也就是控制器B,直接给控制器B传递数据
  • 在B的viewDidLoad方法中取得数据,或者利用setter方法,设置界面上的UI控件;

          eg: 将当前的控制器中的self.modelArray[indexPath.row]数据传递给EditViewController:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
EditViewController *editVc = (EditViewController *)segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
editVc.model = self.modelArray[indexPath.row];
}

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

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781