一 :main函数入口
看下项目资源结构,其实程序的入口也是在main.m里面。
#import <UIKit/UIKit.h>#import "BvinAppDelegate.h"int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([BvinAppDelegate class]));
}
}
UIApplicationMain第四个参数,就是Application的代理类相当于Android中的Application类。
二:AppDelegate代理类
AppDelegate是管理ios程序生命周期的类。
#import <UIKit/UIKit.h>@class BvinViewController;@interface BvinAppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) BvinViewController *viewController;@end
BvinAppDelegate继承UIResponder实现UIApplicationDelegate协议,里面有相应的生命周期的回掉方法。
看下最重要的一个方法application:didFinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions//相当于android中的OnCreate()
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[BvinViewController alloc] initWithNibName:@"BvinViewController" bundle:nil] autorelease];//相当于android中setContentView(xib);
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
这个AppDelegate里有个UIWindow和ViewController,window窗口要设置一个根View的Controller,把想要设置进入首页的View赋值给self.window.rootViewController ,就可以启动你想要的主界面了。
看下这个window是怎么出来的,通过[UIWindow alloc] initWithFrame:方法把屏幕的一个Frame空间设置位整个应用程序的窗口。[UIScreen mainScreen] bounds]这个就是指主屏幕的Frame。
然后这个viewController是通过initWithNibName来加载xib布局,这样就可以把xib视图加载加入进这个viewController。
最后在把这个window的根ViewController设置为该viewController,这样就完成启动主界面的工作了。
UIApplicationDelegate的生命周期方法:
1、application didFinishLaunchingWithOptions:当应用程序启动时执行,应用程序启动入口,只在应用程序启动时执行一次。若用户直接启动,lauchOptions内无数据,若通过其他方式启动应用,lauchOptions包含对应方式的内容。
2、applicationWillResignActive:在应用程序将要由活动状态切换到非活动状态时候,要执行的委托调用,如 按下 home 按钮,返回主屏幕,或全屏之间切换应用程序等。
3、applicationDidEnterBackground:在应用程序已进入后台程序时,要执行的委托调用。
4、applicationWillEnterForeground:在应用程序将要进入前台时(被激活),要执行的委托调用,刚好与applicationWillResignActive 方法相对应。
5、applicationDidBecomeActive:在应用程序已被激活后,要执行的委托调用,刚好与applicationDidEnterBackground 方法相对应。
6、applicationWillTerminate:在应用程序要完全推出的时候,要执行的委托调用,这个需要要设置UIApplicationExitsOnSuspend的键值。
三:UIViewController
这里说下这里有个视图加载的回调。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
视图刚加载的时候会回掉这个方法可以在里面 做些初始化的action,类似于百度地图有个地图加载完成的回掉方法,android的普通的View是没有的。