• 视图控制器创建的多种方式


    "【什么是视图控制器】"

    (1)视图控制器就是用来控制或者说管理界面(视图)

    "换句话说界面长得丑与美由视图控制器说的算

    (2)每个视图控制器(UIViewController)都有一个View属性来描述界面长什么样

    (3)前面所讲的设置窗口的根控制器其实就是会把视图控制器的里View会添加在窗口上进行显示

     

     

    "【视图控制器的创建方式】"

    (1)UIStoryboard的方式

      * 获取storyboard箭头所指的视图控制器(一定要与控制器类相对应)

      * 获取storyboard标识了ID所指的视图控制器

     

    (2)直接创建控制器

     

    (3)创建带xib的视图控制器

      * xib也是描述界面长什么样的

      * 调用控制器的【-(instancetype)initWithNibName:bundle:;

      * 方法为什么是nib,因为程序打包运行后,xib会放在bundle目录下,并且后缀名为nib"进入沙盒验证"

      * xib "File's Owner" 是代表界面由谁管理,控制器view对应哪个界面的描述

     

     

    "【带xib的视图控制器创建细节】"

    /*前言: 使用BlueViewControler为例*/

    (1)使用init方法创建控制器时(一定要与控制器类相对应)

      * 如果当前项目中 '' 与类名相同的xib,会加载xib的界面成功控制器的视图,

      * 如果当前项目中 '没有' 与类名相同的xib,就返回一个白色的View

      //: 演示删除xib时要删除应用程序,项目要clean下,因为有缓存"

     

    (2)创建同类名的xibBlueViewControler.xib,如果没有指定 "file's owner" view连线哪个view,会报错   "(此处要仅记)"

    //此外应贴图

    "报错原因:找不到控制器View的连线

     

    (3)如果创建BlueView.xib,控制器的view会先加载BlueView.xib,如果没有BlueView.xib,再找BlueViewControler.xib,如果BlueViewControler.xib也没有,就自己创建一个空白的界面

     

    (4)loadView方法--初始化控制器的view

        *空实现,不返回view

        *self.view = UITableView 

     

    ApplDelegate.m

    //

    //  AppDelegate.m

    //  02.控制器创建方式

    //

    //  Created by huan on 16/1/17.

    //  Copyright © 2016 huanxi. All rights reserved.

    //

     

    #import "AppDelegate.h"

    #import "OneViewController.h"

    @interface AppDelegate ()

     

    @end

     

    @implementation AppDelegate

     

     

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        // Override point for customization after application launch.

        //创建窗口

        UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    //    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];//为什么为nil,默认就从主bundle获取,story是资源文件

        //获取箭头所指的控制器 (右面有个属性 Is Initial View Controller)

    //    id vc = [mainStoryboard instantiateInitialViewController];

    //    id vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"redvc"];//通过标示获取storyboard的控制器

        

        

        //直接创建根控制器

    //    UIViewController *vc = [[UIViewController alloc] init];

    //    vc.view.backgroundColor = [UIColor greenColor];

        

        

        

        //使用xib方式创建控制器

    //    OneViewController *vc = [[OneViewController alloc] initWithNibName:@"OneViewController" bundle:nil];//nib就是xib。编译打包文件时就变成nib了。

        

        //如果不指定xib,默认会先打同名但是没有controller后缀的xib,如果找不到,再找同名的xib去加载控制器view,在iOS9以后是先找带Controller后缀的xib,然后找不带Controller的xib,如果都没有,返回时灰色的view;

        OneViewController *vc = [[OneViewController alloc] init];

        //设置根控制器

        window.rootViewController = vc;

        //设置主窗口并可见

        [window makeKeyAndVisible];

        self.window = window;//赋值

        

        

        

                            

                            

                            

        return YES;

    }

     

    - (void)applicationWillResignActive:(UIApplication *)application {

        // 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.

        // 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.

    }

     

    - (void)applicationDidEnterBackground:(UIApplication *)application {

        // 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.

        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

    }

     

    - (void)applicationWillEnterForeground:(UIApplication *)application {

        // 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.

    }

     

    - (void)applicationDidBecomeActive:(UIApplication *)application {

        // 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.

    }

     

    - (void)applicationWillTerminate:(UIApplication *)application {

        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

    }

     

    @end

    OneViewController.m

     

    //  OneViewController.m

    //  02.控制器创建方式

    //

    //  Created by huan on 16/1/17.

    //  Copyright © 2016 huanxi. All rights reserved.

    //

     

    #import "OneViewController.h"

     

    @interface OneViewController ()

     

    @end

     

    @implementation OneViewController

    //加载view的时候,会调用这个方法,初始化控制器view

    -(void)loadView{

        //load方法会加载view

        [super loadView];

        NSLog(@"%s", __func__);

        self.view = [[UITableView alloc] init];

        //self.view 实际访问一个view,如果view不存在,它就会调用loadview去加载控制器的view

    //    self.view.backgroundColor = [UIColor grayColor];

    }

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view from its nib.

        NSLog(@"%s", __func__);

     

    }

     

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

     

    /*

    #pragma mark - Navigation

     

    // In a storyboard-based application, you will often want to do a little preparation before navigation

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

        // Get the new view controller using [segue destinationViewController].

        // Pass the selected object to the new view controller.

    }

    */

     

    @end

     

    "【总结视图控制器的View创建的流程】"

    如果控制器是从storyboard创建,loadViewstoryboard加载

    如果控制器是自己创建,加载xib,没有xib,自己创建一个空白的view

     

     

  • 相关阅读:
    WCF之ABC
    一次性为自定义实体类的数据填充
    在HttpHandlers中使用Session
    ASP.NET 2.0防止同一用户同时登陆
    Winson.Framework 1.5发布!
    SqlPager分页控件的使用!
    ExtJS 学习心得(一)
    [原创]Discuz!NT1.1高亮代码插件1.5稳定版!
    Winson.Framework 1.0发布!
    一个不错的WEB打印解决方案!
  • 原文地址:https://www.cnblogs.com/Lu2015-10-03/p/5137795.html
Copyright © 2020-2023  润新知