hello,world不使用ARC

时间:2022-04-27 06:58:01

main.m

//
// main.m
// Hello
//
// Created by lishujun on 14-8-28.
// Copyright (c) 2014年 lishujun. All rights reserved.
// #import <UIKit/UIKit.h> // 视图控制器对象
@interface HelloWorldViewController : UIViewController
@end @implementation HelloWorldViewController -(void) loadView
{
/*
self.view = contentView;
[self setView contentView];
等价,设置属性其实是调用set方法
*/ //创建视图对象
UIView *contentView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];
NSLog(@"new contentView : %d", [contentView retainCount]); // new contentView : 1
contentView.backgroundColor = [UIColor lightGrayColor];
NSLog(@"set contentView : %d", [contentView retainCount]); // set contentView : 1
[self setView: contentView];
NSLog(@"add contentView : %d", [contentView retainCount]); // add contentView : 2 //创建label对象
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 30.0)];
NSLog(@"new label : %d", [label retainCount]); // new label : 1
label.text = @"Hello World";
label.center = contentView.center;
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor redColor];
NSLog(@"set label : %d", [label retainCount]); // set label : 1 //在视图上添加label
[contentView addSubview:label];
NSLog(@"add label : %d", [label retainCount]); // add label : 2
[label release];
NSLog(@"release label : %d", [label retainCount]); // release label : 1 } @end // 委托对象
@interface HelloWorldAppDelegate : NSObject <UIApplicationDelegate>
{
IBOutlet UIWindow *window;
} @property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) HelloWorldViewController *viewController;
//window 必须声明为属性,声明为局部变量则无法绘制视图,显示为黑屏
//apple 官方文档把viewController也声明为属性了
@end @implementation HelloWorldAppDelegate @synthesize window;
@synthesize viewController; -(void) applicationDidFinishLaunching:(UIApplication *)application
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
self.viewController = [[HelloWorldViewController alloc]init];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
} @end // 程序入口
int main(int argc, char * argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, @"HelloWorldAppDelegate");
}
}

输出:

2014-08-30 11:47:30.980 HelloNOARC[562:60b] new contentView : 1
2014-08-30 11:47:30.983 HelloNOARC[562:60b] set contentView : 1
2014-08-30 11:47:30.984 HelloNOARC[562:60b] add contentView : 2
2014-08-30 11:47:30.985 HelloNOARC[562:60b] new label : 1
2014-08-30 11:47:30.986 HelloNOARC[562:60b] set label : 1
2014-08-30 11:47:30.986 HelloNOARC[562:60b] add label : 2
2014-08-30 11:47:30.987 HelloNOARC[562:60b] release label : 1