IOS应用程序第一个项目HelloWorld(一)

具体创建项目的方法与开发语言相关知识请阅读我之前的文章 本系列文章将着重讨论iPhone与iPad 应用程序入门的开发。


创建一个iPhone项目

名称为HelloWorld,如图所示彩色矩形标示的为一些项目中比较重要的组成部分。
HelloWorldAppDelegate:监听应用程序的周期,比如程序的启动,程序内存的警告,程序挂起后在恢复等等。
HelloWorldViewController: 控制应用程序的显示,跟视图有关的东西都在这里。
MainWindows.xib与HelloWorldViewController.xib: 可视化界面的布局,方便界面的设计,一会儿介绍它的使用方法。
main:最重要的一个类,iPhone程序的入口类。
接下来介绍这几各类重要的一些方法。
打开main.m这个入口类
UIApplicationMain函数:这个函数是iPhone应用开发的入口,在这里实例化应用程序,通知HelloWorldAppDelegate启动应用程序。
1 #import <UIKit/UIKit.h>
2  
3 int main(int argc, char *argv[])
4 {
5     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
6     int retVal = UIApplicationMain(argc, argv, nil, nil);
7     [pool release];
8     return retVal;
9 }

 

HelloWorldAppDelegate.m
应用程序启动后进入这个方法,在这里创建程序的窗口,启动结束以后HelloWorldAppDelegate就不会在发挥作用 ,除非程序出现特殊情况,比如内存警告,程序的挂起与恢复 等等  正常情况下 程序会启动视图控制器,通知HelloWorldViewController去显示这个应用程序。
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2 {
3     // Override point for customization after application launch.
4  
5     self.window.rootViewController = self.viewController;
6     [self.window makeKeyAndVisible];
7     return YES;
8 }

 

HelloWorldViewController.m

程序界面显示之前调用,用于初始化显示内容。
1 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
2 - (void)viewDidLoad
3 {
4     [super viewDidLoad];
5 }

 

 

下面构建第一个HelloWorld项目

 

首先打开 HelloWorldViewController.xib显示控制器,这时候IDE出现一个可视化编辑视图。
视图控件栏中存在很多控件 可以拖放使用,举个例子拖动右侧Lable控件至可视化编辑视图中,可放置任意位置,编辑Lable控件显示内容。
在编辑框中写入HelloWorld后保存,一行代码都不用添加直接运行程序。HelloWorld映入眼帘,简单吧 哇咔咔。

下面用代码来添加Lable控件

HelloWorldViewController.m
01 - (void)viewDidLoad
02 {
03     [super viewDidLoad];
04  
05     //创建label视图
06     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 200, 30)];
07     //设置显示内容
08     label.text = @"雨松MOMO的程序世界";
09     //设置背景颜色
10     label.backgroundColor = [UIColor blueColor];
11     //设置文字颜色
12     label.textColor = [UIColor whiteColor];
13     //设置显示位置居中
14     label.textAlignment = UITextAlignmentCenter;
15     //设置字体大小
16     label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20];
17     //将label添加到显示视图中
18     [self.view addSubview:label];
19  
20     //释放对象
21     [label release];
22 }

运行程序 如图所示 使用视图添加的文本内容 与使用代码添加的文本内容都出现在了屏幕当中。

 

 

使用可视化布局添加一个按钮

01 #import <UIKit/UIKit.h>
02  
03 @interface HelloWorldViewController : UIViewController
04 {
05     //定义了一个按钮buttonA
06     IBOutlet UIButton *buttonA;
07     //定义了一个按钮buttonB
08     UIButton *buttonB;
09  
10 }
11 //声明A按钮被按下的一个方法(IBAction) 相当于(void)
12 -(IBAction)bttonPressed:(id)obj;    
13  
14 -(void)showDialog;
15 @end

在右侧栏中点中 鼠标 New Referencing Outlet 拉出一条线到左侧箭头指示方块上松开鼠标 这是会显示上面定义的IBOutlet UIButton ,选中这个buttonA 标示这个拖动的button控件和buttonA 绑定在了一起,然后鼠标点击右侧Touch up inside 同样拉出一条线到左侧箭头指示方块上松开鼠标 这时候会显示上面定义的方法buttonPressed 选中后 则标示 这个按钮点击后程序执行buttonPressed方法。

 

 

将按钮的事件绑定后在代码中去实现这个方法,点击后调用showDialog 方法弹出一个dialog框。

 

01 - (void)bttonPressed:(id)obj
02 {
03     [self showDialog];
04  
05 }  
06  
07 -(void)showDialog
08 {
09  
10     //这里说一下nil  这个东西就好比java 语言中的 null
11     UIAlertView * alertA= [[UIAlertView alloc] initWithTitle:@"我的视图" message:@"点开了弹出对话框" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
12     //objectiveC开发中调用方法是用"[]" 例如: [alertA addButtonWithTitle:@"取消"];
13     //如果是为方法赋值则类似java 对象.成员 例如 :textFieldA.text
14     //添加了一个取消按钮
15     [alertA addButtonWithTitle:@"取消"];
16     //将这个UIAlerView 显示出来
17     [alertA show];
18     //objective-C 不像java 有自己的垃圾回收机制 所以我们在编写程序中一定要注意释放内存 从一开始就养成良好习惯
19     [alertA release];
20 }

 

 

这个方法为view视图在销毁的时候调用,所以在这里释放按钮对象。

 

01 - (void)viewDidUnload
02 {
03     [super viewDidUnload];
04     // Release any retained subviews of the main view.
05     // e.g. self.myOutlet = nil; 
06  
07     //在这里将按钮对象释放掉
08     [self.buttonA release];
09     [self.buttonB release];
10 }

完整代码

01 //
02 //  HelloWorldViewController.m
03 //  HelloWorld
04 //
05 //  Created by  雨松MOMO on 11-9-30.
06 //  Copyright 2011年 __MyCompanyName__. All rights reserved.
07 //
08  
09 #import "HelloWorldViewController.h"
10  
11 @implementation HelloWorldViewController
12  
13 - (void)didReceiveMemoryWarning
14 {
15     // Releases the view if it doesn't have a superview.
16     [super didReceiveMemoryWarning];
17  
18     // Release any cached data, images, etc that aren't in use.
19 }
20  
21 #pragma mark - View lifecycle
22  
23 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
24 - (void)viewDidLoad
25 {
26     [super viewDidLoad];
27  
28     //创建label视图
29     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 200, 30)];
30     //设置显示内容
31     label.text = @"雨松MOMO的程序世界";
32     //设置背景颜色
33     label.backgroundColor = [UIColor blueColor];
34     //设置文字颜色
35     label.textColor = [UIColor whiteColor];
36     //设置显示位置居中
37     label.textAlignment = UITextAlignmentCenter;
38     //设置字体大小
39     label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20];
40     //将label添加到显示视图中
41     [self.view addSubview:label];
42  
43     //释放对象
44     [label release];
45  
46     //创建按钮
47     buttonB = [UIButton buttonWithType:1];
48     //设置按钮范围
49     buttonB.frame = CGRectMake(100, 250, 150, 30);
50     //设置按钮显示内容
51     [buttonB setTitle:@"代码添加按钮" forState:UIControlStateNormal];
52     //设置按钮改变后 绑定响应方法
53     [buttonB addTarget:self action:@selector(showDialog) forControlEvents:UIControlEventTouchUpInside];
54  
55     //将label添加到显示视图中
56     [self.view addSubview:buttonB];
57  
58 }
59  
60 - (void)viewDidUnload
61 {
62     [super viewDidUnload];
63     // Release any retained subviews of the main view.
64     // e.g. self.myOutlet = nil;
65  
66     //在这里将按钮对象释放掉
67     [buttonA release];
68     [buttonB release];
69  
70 }
71  
72 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
73 {
74     // Return YES for supported orientations
75     return (interfaceOrientation == UIInterfaceOrientationPortrait);
76 }
77  
78 - (void)bttonPressed:(id)obj
79 {
80     [self showDialog];
81  
82
83  
84 -(void)showDialog
85 {
86  
87     //这里说一下nil  这个东西就好比java 语言中的 null
88     UIAlertView * alertA= [[UIAlertView alloc] initWithTitle:@"我的视图" message:@"点开了弹出对话框" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
89     //objectiveC开发中调用方法是用"[]" 例如: [alertA addButtonWithTitle:@"取消"];
90     //如果是为方法赋值则类似java 对象.成员 例如 :textFieldA.text
91     //添加了一个取消按钮
92     [alertA addButtonWithTitle:@"取消"];
93     //将这个UIAlerView 显示出来
94     [alertA show];
95     //objective-C 不像java 有自己的垃圾回收机制 所以我们在编写程序中一定要注意释放内存 从一开始就养成良好习惯
96     [alertA release];
97 }
98  
99 @end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
// // main.m // Hello World_Code // #import #import "AppDelegate.h" int main(int argc, char *argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } } // // AppDelegate.h // Hello World_Code // #import @interface AppDelegate : UIResponder @property (strong, nonatomic) UIWindow *window; @end // // AppDelegate.m // Hello World_Code // #import "AppDelegate.h" @implementation AppDelegate - (void)dealloc { [_window release]; [super dealloc]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(320/2.0 - 140/2.0, 80, 140, 40)]; // 1 label.text = @"Hello World"; label.backgroundColor = [UIColor cyanColor]; label.textAlignment = NSTextAlignmentCenter; [self.window addSubview:label]; // 2 [label release]; // 1 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值