Snail—UI学习之初识
在AppDelegate.m中有几个默认存在的函数
// // WJJAppDelegate.m // 课上练习 // // Created by Snail on 15-7-20. // Copyright (c) 2015年 Snail. All rights reserved. // #import "WJJAppDelegate.h" @implementation WJJAppDelegate //程序的入口 仅仅同意一次 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // [[UIScreen mainScreen] bounds]]; 适应当前屏幕的大小 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. //设置window的背景颜色为白色 self.window.backgroundColor = [UIColor whiteColor]; //让window运行显示 [self.window makeKeyAndVisible]; 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. } //当前APP已经可以看到界面 进入前台 - (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
这样我们知道了程序的入口,可是在以后的开发过程中,我们不会直接在AppDelegate.m中写非常多的类,而是新建一个UIViewController,把自己创建的一个UIViewController赋值给我们此程序的根视图控制器
接下来,COMMAND+N 新建一个类WJJRootUIViewController 继承于UIViewController
引入头文件后,在以下这种方法这样写
#import "WJJAppDelegate.h" #import "WJJRootViewController.h" @implementation WJJAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //当前的window 开辟内存 并用frame初始化 //[[UIScreen mainScreen] bounds]] 适应当前屏幕大小 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.rootViewController = [[WJJRootViewController alloc] init]; //设置当前window 的背景色 self.window.backgroundColor = [UIColor cyanColor]; //让这个windo运行 [self.window makeKeyAndVisible]; return YES; }
这种话,我们的根视图控制器变成了WJJRootUIVIewController
其它不动。我们直接到WJJRootUIViewController.m文件里
#import "WJJRootViewController.h" @interface WJJRootViewController () @end @implementation WJJRootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } //此视图控制器的入口 入口仅仅运行一次 其它的进入后台、进入前台等等是在调用以下四个周期函数 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //把背景颜色设置为红色 self.view.backgroundColor = [UIColor redColor]; } //视图已经出现 - (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; } //视图将要出现 - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; } //视图将要消失 - (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; } //视图已经消失 - (void)viewDidDisappear:(BOOL)animated{ [super viewDidDisappear:animated]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end