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

  • 相关阅读:
    【ALearning】第三章 Android基本常见控件
    【问题汇总】ListView的FooterView设置可见性的问题
    shell重定向调试信息
    Android提供的系统服务之--TelephonyManager(电话管理器)
    工作一年的总结
    【甘道夫】HBase基本数据操作的详细说明【完整版,精绝】
    未定义标识符_ConnectionPtr
    POJ 1329 三角外接圆
    IOS开发-加载本地音乐
    SQL Server 性能优化3 该指数(Index)保养
  • 原文地址:https://www.cnblogs.com/wangshuo1/p/5469813.html
Copyright © 2011-2022 走看看