首页 技术 正文
技术 2022年11月10日
0 收藏 884 点赞 3,973 浏览 2340 个字

【IOS6.0 自学瞎折腾】(五)应用程序的启动过程和Application生命周期

一 :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是没有的。


    					
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290