IOS应用程序第一个项目HelloWorld(一)
2013-03-03 15:21
387 查看
具体创建项目的方法与开发语言相关知识请阅读我之前的文章 本系列文章将着重讨论iPhone与iPad 应用程序入门的开发。
创建一个iPhone项目
[b]名称为HelloWorld,如图所示彩色矩形标示的为一些项目中比较重要的组成部分。
[/b]
HelloWorldAppDelegate:监听应用程序的周期,比如程序的启动,程序内存的警告,程序挂起后在恢复等等。
HelloWorldViewController: 控制应用程序的显示,跟视图有关的东西都在这里。
MainWindows.xib与HelloWorldViewController.xib: 可视化界面的布局,方便界面的设计,一会儿介绍它的使用方法。
main:最重要的一个类,iPhone程序的入口类。
接下来介绍这几各类重要的一些方法。
打开main.m这个入口类
UIApplicationMain函数:这个函数是iPhone应用开发的入口,在这里实例化应用程序,通知HelloWorldAppDelegate启动应用程序。
HelloWorldAppDelegate.m
应用程序启动后进入这个方法,在这里创建程序的窗口,启动结束以后HelloWorldAppDelegate就不会在发挥作用 ,除非程序出现特殊情况,比如内存警告,程序的挂起与恢复 等等 正常情况下 程序会启动视图控制器,通知HelloWorldViewController去显示这个应用程序。
HelloWorldViewController.m
程序界面显示之前调用,用于初始化显示内容。
下面构建第一个HelloWorld项目
首先打开 HelloWorldViewController.xib显示控制器,这时候IDE出现一个可视化编辑视图。
视图控件栏中存在很多控件 可以拖放使用,举个例子拖动右侧Lable控件至可视化编辑视图中,可放置任意位置,编辑Lable控件显示内容。
在编辑框中写入HelloWorld后保存,一行代码都不用添加直接运行程序。HelloWorld映入眼帘,简单吧 哇咔咔。
下面用代码来添加Lable控件
HelloWorldViewController.m
运行程序 如图所示 使用视图添加的文本内容 与使用代码添加的文本内容都出现在了屏幕当中。
使用可视化布局添加一个按钮
在右侧栏中点中 鼠标 New Referencing Outlet 拉出一条线到左侧箭头指示方块上松开鼠标 这是会显示上面定义的IBOutlet UIButton ,选中这个buttonA 标示这个拖动的button控件和buttonA 绑定在了一起,然后鼠标点击右侧Touch up inside 同样拉出一条线到左侧箭头指示方块上松开鼠标 这时候会显示上面定义的方法buttonPressed 选中后 则标示 这个按钮点击后程序执行buttonPressed方法。
将按钮的事件绑定后在代码中去实现这个方法,点击后调用showDialog 方法弹出一个dialog框。
这个方法为view视图在销毁的时候调用,所以在这里释放按钮对象。
完整代码
view
source
创建一个iPhone项目
[b]名称为HelloWorld,如图所示彩色矩形标示的为一些项目中比较重要的组成部分。
[/b]
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 |
7 | [pool release]; |
8 | return retVal; |
9 | } |
应用程序启动后进入这个方法,在这里创建程序的窗口,启动结束以后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 | } |
程序界面显示之前调用,用于初始化显示内容。
1 | // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. |
2 | - ( void )viewDidLoad |
3 | { |
4 | [super viewDidLoad]; |
5 | } |
首先打开 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 |
将按钮的事件绑定后在代码中去实现这个方法,点击后调用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 | } |
view
source
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 |
相关文章推荐
- IOS研究之应用程序第一个项目HelloWorld
- 越狱开发之——iOSOpenDev建立第一个越狱项目HelloWorld插件
- [iOS] 创建第一个应用程序项目
- Android第一个应用程序helloworld项目详解
- Android第一个应用程序helloworld项目详解
- cocos2dx第一个游戏项目helloworld(二)
- IOS学习笔记(一)之安装开发环境与工具与第一个IOS应用程序
- Java转iOS-第一个项目总结(2):遇到问题和解决方案
- 使用springboot创建第一个helloworld项目
- Three20软件引擎之构建开发环境与第一个项目HelloWorld(一)
- 从Java转iOS第一个项目总结(常用第三方,工具介绍)
- IOS 入门开发之构建第一个应用程序(一)
- [IOS]从零开始搭建基于Xcode7的IOS开发环境和免开发者帐号真机调试运行第一个IOS程序HelloWorld
- ios学习笔记(一)xcode 4.3.2下创建第一个ios项目
- 创建第一个IOS项目
- Android学习3--第一个Android项目HelloWorld的建立及剖析
- iOS7初体验(1)——第一个应用程序HelloWorld
- IOS学习笔记(一)之安装开发环境与工具与第一个IOS应用程序
- quick-cocos2d-x教程1:在window上创建第一个项目文件夹,并制作helloworld
- Silverlight 2 实验手册之一 创建第一个Hello World 应用程序(附示例项目下载)