Iphone开发(二)从helloWorld看app应用的运行轨迹和生命周期
又要helloWorld了,每一个编程语言都逃不了这个宿命,今天我们通过helloworld来简单看一下iphone应用的生命周期和运行轨迹。
首先创建一个target:
建好之后我们会发现生成
好了,下面我们来对照生命周期图来研究这个项目:
首先,main方法是程序的入口,我们在main.m中会看到这样的代码:
[cpp] #import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
//NSStringFromClass 其实只是得到AppDelegate类名。
}
}
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
//NSStringFromClass 其实只是得到AppDelegate类名。
}
}
在main中执行一个方法UIApplicationMain,该方法返回一个UIApplication对象表示我们的这个应用,第四个参数是一个字符串,提示我们去该字符串表示的类中寻找下一步的线索:application:DidFinishLaunchingWithOptions。我们发现该类名为AppDelegate,现在我们找到该类发现里面是如下内容:
[plain] #import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];//1
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];//2
self.window.rootViewController = self.viewController;//3
[self.window makeKeyAndVisible];//4
return YES;
}
@end
#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];//1
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];//2
self.window.rootVie
相关新闻>>
- 发表评论
-
- 最新评论 更多>>