zoukankan      html  css  js  c++  java
  • iOS:视图切换的第二种方式:UINavigationController导航栏控制器

    UINavigationController:一个以栈的形式管理多视图的容器,负责子控制器之间的跳转。由于以栈的方式管理视图,各个视图的切换就是压栈和出栈操作,所以出栈后的视图会立即销毁。

    介绍:

    <1>
    UINavigationController也是容器的一种,负责子控制器之间的跳转
    <2>
    UINavigationController以栈的形式保存子控制器,这里的栈其实是个NSArray对象,只有位于栈顶的子控制器的view才是可见的
    <3>
    常用属性:
    –UIViewController *topViewController 处于栈顶的子控制器
    –NSArray *viewControllers 栈,存放着所有的子控制器
    <4>
    UINavigationController的view包括了两大部分:
    上面是UINavigationBar导航栏,里面放着导航栏标题和navigationItem导航条。
    下面是topViewController的view,topViewController的navigationItem属性决定了UINavigationBar显示的内容
    <5>除了导航栏中的title,UINavigationItem还有一些属性能够影响UINavigationBar的显示内容
    –UIBarButtonItem *backBarButtonItem 左上角的返回按钮
    –UIView *titleView   中间的标题视图
    –UIBarButtonItem *leftBarButtonItem  左上角的视图
    –UIBarButtonItem *rightBarButtonItem  右上角的视图
              导航栏标题Title设置
     
                导航条navigationItem中属性的(左按钮、右按钮、标题视图)设置
     
     
     
    <6>
    UINavigationController的pushViewController:animated:方法可以将一个控制器压入栈中
    子控制器都有个navigationController属性,可以获取当前的UINavigationController对象
    假如从子控制器c1跳转到子控制器c2,可以这样做:

    [c1.navigationController pushViewController:c2 animated:YES];

    <7>

    纯代码创建多视图的步骤:

    1、首先将项目中的storyboard删除,并将项目栏General中Main interface的设置取消。

    2、在应用程序代理类中创建window并设置它的frame,一般将window的大小设置为单例对象屏幕的大小,然后给window设置根控制器。

    3创建UINavigationController控制器,它是一个容器,用- (instancetype)initWithRootViewController:(UIViewController *)rootViewController方法初始化,传入一个UIViewController实例作为根控制器,这个根控制器永远处于栈底(如果栈中只有一个对象,那么根控制器也处于栈顶),将某个UIViewController压入栈时,控制器的视图会从窗口右侧滑入;出栈时,栈顶的控制器会被移除,其下的控制器的视图会从窗口左侧滑入

    3、如果还要创建其他多个控制器,创建后,必须要获取当前栈中的根控制器,根控制器用- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animate方法将这些控制器依次推入栈中。

    4、针对每一个控制器,在它们的类中分别设置它们的导航栏标题和导航条navigationItem中的属性,例如添加按钮、设置标题视图等,添加按钮时,给按钮初始化时可以设置类型,并添加事件,在事件中用方法:- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated推出栈中它的前一个控制器。

      

      纯代码创建举例如下:

    显示结果为:

     

      文件截图为:

     

    取消general中的Main interface设置

     

       代码如下

    应用程序代理类中创建window和根控制器

     1 #import "AppDelegate.h"
     2 #import "ViewController.h"
     3 
     4 @interface AppDelegate ()
     5 
     6 @end
     7 
     8 @implementation AppDelegate
     9 
    10 
    11 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    12     //创建window
    13     self.window = [[UIWindow alloc]init];
    14     
    15     //设置window的大小
    16     CGSize screenSize = [[UIScreen mainScreen]bounds].size;
    17     self.window.frame = CGRectMake(0, 0, screenSize.width, screenSize.height);
    18     
    19     //为window设置根控制器
    20     ViewController *VC = [[ViewController alloc]init];
    21     UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:VC];
    22     
    23     self.window.rootViewController = naVC;
    24     
    25     //接收用户输入并显示
    26     [self.window makeKeyAndVisible];
    27     
    28     return YES;
    29 }
    30 
    31 - (void)applicationWillResignActive:(UIApplication *)application {
    32     // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    33     // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    34 }
    35 
    36 - (void)applicationDidEnterBackground:(UIApplication *)application {
    37     // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    38     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    39 }
    40 
    41 - (void)applicationWillEnterForeground:(UIApplication *)application {
    42     // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    43 }
    44 
    45 - (void)applicationDidBecomeActive:(UIApplication *)application {
    46     // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    47 }
    48 
    49 - (void)applicationWillTerminate:(UIApplication *)application {
    50     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    51 }
    52 
    53 @end

    在设置的根控制器类中设置导航栏属性,并将新的视图控制器推入栈中

     1 #import "ViewController.h"
     2 #import "secondViewController.h"
     3 
     4 @interface ViewController ()
     5 
     6 @end
     7 
     8 @implementation ViewController
     9 
    10 - (void)viewDidLoad {
    11     [super viewDidLoad];
    12     [self.view setBackgroundColor:[UIColor yellowColor]];
    13     
    14     //设置导航栏
    15     //设置标题
    16     self.navigationItem.title = @"First";
    17     
    18     //设置返回按钮
    19     self.navigationItem.backBarButtonItem.title = @"first";
    20     
    21     //设置右边的按钮
    22     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addClicked:)];
    23 }
    24 
    25 -(void)addClicked:(UIBarButtonItem*)sender
    26 {
    27     secondViewController *secondVC = [[secondViewController alloc]init];
    28     
    29     //将新的控制器压入导航控制器
    30     [self.navigationController pushViewController:secondVC animated:YES];
    31 }
    32 
    33 @end

    在新的视图控制器类中设置导航栏属性,将当前控制器弹出栈中

     1 #import "secondViewController.h"
     2 
     3 @interface secondViewController ()
     4 
     5 @end
     6 
     7 @implementation secondViewController
     8 
     9 - (void)viewDidLoad {
    10     [super viewDidLoad];
    11     self.view.backgroundColor = [UIColor redColor];
    12     
    13     //设置导航栏
    14     //1、设置标题
    15     self.navigationItem.title = @"Second";
    16     
    17     //2.设置左边的按钮
    18     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Return" style:UIBarButtonItemStylePlain target:self action:@selector(backClicked:)];
    19 }
    20 
    21 -(void)backClicked:(UIBarButtonItem*)sender
    22 {
    23     //将当前的控制器弹出导航控制器
    24     [self.navigationController popToRootViewControllerAnimated:YES];
    25 }
    26 @end
  • 相关阅读:
    正则表达式匹配中国区的电话号码
    落户
    存储过程与函数的区别转载
    asp.net页面从初始化到卸载事件顺序转
    .net连接Oracle数据库 常见问题
    sql触发器的类型
    (转) C++中extern “C”含义深层探索
    vmware 收缩越来越大的磁盘文件
    (转)一些常用的网站
    工具:P/Invoke Interop Assistant
  • 原文地址:https://www.cnblogs.com/XYQ-208910/p/4801186.html
Copyright © 2011-2022 走看看