zoukankan      html  css  js  c++  java
  • 轻量级应用开发之(09)创建控制器

    一 创建控制器

    第1种方式 通过代码控制器

    HKUIViewController.h

    #import <UIKit/UIKit.h>
    
    @interface HKUIViewController : UIViewController
    
    @end

    HKUIViewController.m

    #import "HKUIViewController.h"
    
    @implementation HKUIViewController
    
    @end

    AppDelegate.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc]init] ;
        HKUIViewController * hkvc = [[HKUIViewController alloc]init];
        hkvc.view.backgroundColor = [UIColor redColor];
        self.window.rootViewController = hkvc;
        [self.window makeKeyAndVisible];
        
        return YES;
    }

    第2种方式: 通过storyboard加载控制器。

     1) XCode创建storyboard

      Xcode -> iOS -> User interface -> Storyboard 新建一个名为 Test.storyboard文件。

    2) 设置Test.storyboard为 initial View Controller

      勾选中 is initial View Controller.

     

    3)拖拽一个 View Controller到 Test.storyboard中。  

       设置他的class为 HKUIViewController。

    AppDelegate.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc]init] ;
       
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Test" bundle:nil];
        HKUIViewController * hkvc =  [storyboard instantiateInitialViewController];
        
        self.window.rootViewController = hkvc;
        [self.window makeKeyAndVisible];
        
        return YES;
    }

     也可以通过表示加载控制器。

    在 Test.storyboard中 identity设置Storyboard ID为 switch。

    AppDelegate.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc]init] ;
       
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Test" bundle:nil];
       // HKUIViewController * hkvc =  [storyboard instantiateInitialViewController];
        HKUIViewController *hkvc = [storyboard instantiateViewControllerWithIdentifier:@"switch"];
        
        self.window.rootViewController = hkvc;
        [self.window makeKeyAndVisible];
        
        return YES;
    }

    第三种方式:通过xib加载控制器

    1)通过XCode创建 Xib

       XCode -> iOS -> User Interface -> View, 建立一个名为Test.xib的xib .

    2) 设置 xib的文件所有者。

      设置 Test.xib的 File's Owner 的 class。

          设置 Test.xib的 File's Owner 的 view关联。

     

     AppDelegate.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc]init] ;
    
        HKUIViewController *hkvc = [[HKUIViewController alloc]initWithNibName:@"Test" bundle:nil ];
        
        self.window.rootViewController = hkvc;
        [self.window makeKeyAndVisible];
        
        return YES;
    }

    也可以创建控制器的同时创建一个xib

    1)Xcode 创建控制器

    知识点:

    commond + n 新建一个storyboard

  • 相关阅读:
    CentOS 7 SSH远程证书登陆
    Keepalived安装配置入门
    Docker-Compose 一键部署Ningx+.Net Core+Redis集群
    .Net Core Cookie跨站点共享 会话保持
    .Net Core EF Core之Sqlite使用及部署
    CentOS 7 Fail2ban防暴力破解
    CentOS 7 Nginx安装配置
    CentOS 7 Firewalld 常用操作
    Linux 修改SSH端口及禁用ROOT远程SSH登陆
    Mysql MariaDB安装
  • 原文地址:https://www.cnblogs.com/wangshuo1/p/5469813.html
Copyright © 2011-2022 走看看