轻量级应用开发之(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
一个知识点,你自己看懂了,那是一个层次;你会用,是另外一个层次;你写出来,你写出来让别人懂,那又是更高的一个层次;你用最通俗的言语,把知识点讲出来,让别人一下子明白,又是更高的一个层次。
如果您觉的本篇文章有用,可以赞助作者一些小额的比特币,用来买咖啡,谢谢。 收款地址:3NTPbsJKRKhe1RE1g2rZdr2dFTDgkBUgUa
注:转载需注明出处及作者名,严禁恶意转载,尊重原作者的劳动成果。
如果您觉的本篇文章有用,可以赞助作者一些小额的比特币,用来买咖啡,谢谢。 收款地址:3NTPbsJKRKhe1RE1g2rZdr2dFTDgkBUgUa
注:转载需注明出处及作者名,严禁恶意转载,尊重原作者的劳动成果。