zoukankan      html  css  js  c++  java
  • iOS开发UI篇—控制器的创建

    iOS开发UI篇—控制器的创建

    说明:控制器有三种创建方式,下面一一进行说明。

    一、第一种创建方式(使用代码直接创建)

    1.创建一个空的IOS项目。

    2.为项目添加一个控制器类。

     

    3.直接在代理方法中创建一个控制器。

    #import "YYAppDelegate.h"
    #import "YYViewController.h"
    
    @implementation YYAppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.window.backgroundColor = [UIColor whiteColor];
        
        //创建控制器
        YYViewController *controller=[[YYViewController alloc]init];
        //设置控制器View的背景颜色
        controller.view.backgroundColor=[UIColor brownColor];
        //设置该控制器为Window的根控制器
        self.window.rootViewController=controller;
        [self.window makeKeyAndVisible];
        return YES;
    }

    4.控制器的view添加到Window上显示出来。

     

    二、第二种创建方式(通过storyboard创建)

    1.添加一个storyboard文件,命名为test,在界面上拖一个view controller控制器。

    2.设置这个控制器和程序中的YYviewController类进行关联,设置控制器view的颜色以便区分。

    3.注意这种错误写法。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.window.backgroundColor = [UIColor whiteColor];
        
        YYViewController *controller=[[YYViewController alloc]init];
        self.window.rootViewController=controller;
        
        [self.window makeKeyAndVisible];
        return YES;
    }

    注意:仅仅这样是不行的,因为程序中并没有加载storyboard,所以没有能够创建出我们想要的控制器。怎么办?明确的告诉要加载的storyboard。

    4.创建代码:

    #import "YYAppDelegate.h"
    #import "YYViewController.h"
    
    @implementation YYAppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.backgroundColor = [UIColor whiteColor];
        
    //    YYViewController *controller=[[YYViewController alloc]init];
        
        //1.加载storyboard,(注意:这里仅仅是加载名称为test的storyboard,并不会创建storyboard中的控制器和控件)
        UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"test" bundle:nil];
        
        //2.下面这个方法代表着创建storyboard中箭头指向的控制器(初始控制器)
        YYViewController *controller=[storyboard instantiateInitialViewController];
        
        //参考
     //   UINib *nib=[UINib nibWithNibName:@"test" bundle:nil];
      //  [nib instantiateWithOwner:nil options:nil];
        
        //3.设置控制器为Window的根控制器
        self.window.rootViewController=controller;
        
        [self.window makeKeyAndVisible];
        return YES;
    }

    步骤:

    (1)加载storyboard

    (2)创建控制器

    (3)把控制器设置为Window的根控制器

    5.拓展

    新的需求:如果在一个storyboard中又多个控制器,如何指定创建哪个特定的控制器呢?

    这里有两种方法可以考虑:

    (1)更改初始控制器,即把箭头拖拽到想要创建的控制器前面,在代码中进行创建。

    实现代码:

    #import "YYAppDelegate.h"
    #import "YYViewController.h"
    
    @implementation YYAppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.backgroundColor = [UIColor whiteColor];
        
    //    YYViewController *controller=[[YYViewController alloc]init];
        
        //1.加载storyboard
        UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"test" bundle:nil];
        
        //2.创建指定的控制器
        UIViewController *controller=[storyboard instantiateInitialViewController];
        
        //3.设置控制器为Window的根控制器
        self.window.rootViewController=controller;
        
        [self.window makeKeyAndVisible];
        return YES;
    }

    提示:注意创建的指定控制器的类型。

    (2)通过设置唯一的标识符来创建指定的控制器

    实现代码:

    #import "YYAppDelegate.h"
    #import "YYViewController.h"
    
    @implementation YYAppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.backgroundColor = [UIColor whiteColor];
        
        //1.加载storyboard
        UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"test" bundle:nil];
        
        //2.创建指定的控制器
        
        UIViewController *controller=[storyboard instantiateViewControllerWithIdentifier:@"one"];
        //3.设置控制器为Window的根控制器
        self.window.rootViewController=controller;
        
        [self.window makeKeyAndVisible];
        return YES;
    }

    三、第三种创建方式(使用xib)

    1.新建一个xib文件,命名为two.xib。

    2.创建过程和注意点

    (1)创建代码:

    #import "YYAppDelegate.h"
    #import "YYViewController.h"
    
    @implementation YYAppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.backgroundColor = [UIColor whiteColor];
        
        //1.通过xib创建控制器
        YYViewController *controller=[[YYViewController alloc]initWithNibName:@"two" bundle:nil];
        //2.设置这个控制器为Window的根控制器
        self.window.rootViewController=controller;
        
        [self.window makeKeyAndVisible];
        return YES;
    }

    (2)两个错误注意点

    1)不能加载(was unable to load a nib named "two"

    产生原因:在xib文件中没有进行任何操作。

    解决方法:往xib中拖一个view

    2)加载了xib,但是没有设置输出口(loaded the "two" nib but the view outlet was not set.'

    产生原因:没有把xib的view设置为YYviewController的view

    解决方法:设置File‘s Owner,可以理解为设置这个文件归谁所有,对File‘s Owner和view进行连线。连线是因为一个xib中可能会有多个view,在storyboard中默认就已经进行了连线。

    四、模仿有storyboard的项目控制器的创建

    1.创建一个项目

    2.在配置文件中,把主storyboard的名称删掉(找不到storyboard)。

    3.代码(storyboard做的事情就是下面的代码所做的事情)

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
       
        // Override point for customization after application launch.
        // 1.创建winodw
        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        // 2.创建控制器
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        NJViewController * vc = [storyboard instantiateInitialViewController];
        // 3.设置window的根控制器
        self.window.rootViewController = vc;
        // 4.显示window
        [self.window makeKeyAndVisible];
        
        return YES;
    }
  • 相关阅读:
    【ML-9-1】支持向量机--软硬间隔与支持向量机
    【ML-8】感知机算法-传统和对偶形式
    【ML-7】聚类算法--K-means和k-mediods/密度聚类/层次聚类
    【ML-7】聚类算法-实例代码
    【ML-6-2】集成学习-boosting(Adaboost和GBDT )
    【ML-6-1】集成学习-bagging(随机森林)
    【ML-5】决策树算法
    【ML-4】逻辑回归--用于分类
    【ML-3.1】梯度下降于牛顿法实例
    树状数组
  • 原文地址:https://www.cnblogs.com/yipingios/p/5553581.html
Copyright © 2011-2022 走看看