iPhone起步-1: "Hello,World!" in iPhone Development (转)

来源:互联网 发布:免费股票交易软件下载 编辑:程序博客网 时间:2024/04/20 13:55

1.认识Xcode和Interface Builder(IB)

 

2.MVC泛型
MVC模型将所有功能划分为3种:
模型:保存应用程序数据的类。
视图:窗口、控件和其他用户可以看到并能与之交互的元素的组成部分。
控制器:将模型和试图绑定在一起,确定如何处理用户输入的应用程序逻辑。

 

3.基本概念
属性声明:@property (retain, nonatomic) IBOutlet UIButton *myButton;
属性实现:@synthesize myButton;
输出口(outlet): 控制器类可以使用输出口(通过关键字IBOutlet声明的实例变量)来引用nib中的对象。
操作:nib文件中的界面对象触发控制器类中的特殊方法,通过关键字IBAction声明的方法。例如: - (IBAction) doSomething: (id)sender;

 

4.Hello,World示例
我们将创建一个很简单的iPhone程序作为我们iPhone之旅的"Hello,World".
界面如下图所示,

触摸Button时,将弹出一个提示框(alert),提示框的内容为TextField中的输入。


步骤如下:
启动Xcode,创建一个名为"HelloiPhone",基于iPhone OS | Application | View-based Application项目模板的应用程序。
打开控制器类头文件HelloiPhoneViewController.h的,添加属性和方法声明:

 

//
// HelloiPhoneViewController.h
// HelloiPhone
//
// Created by Elf Sundae on 11/4/10.
// Copyright 2010 Elf.Sundae(at)Gmail.com. All rights reserved.
//

#import 
<UIKit/UIKit.h>

@interface HelloiPhoneViewController : UIViewController
{
UITextField 
* textField;
}

@property (retain, nonatomic) IBOutlet UITextField 
*textField;

- (IBAction) buttonPressed: (id)sender;

@end
复制代码

 

在Groups & Files窗格下的Resources下,双击 HHelloiPhoneViewController.xib ,将打开Interface Builder。

 

选择菜单 Tools | Library(command + Shift + L),从Library下的Objects拖动一个Text Field和一个Round Rect Button到View。双击button改名为“OK”。
按着Ctrl键,从IB主窗口的File's Owner拖一条线到View的Text Field,在弹出的窗口选择textField。
鼠标点选View中button,按command+2或者菜单选择"Tools | Connetions Inspector",从Event中的Touch Up Inside后面的小圆圈拉一条线连接到主窗口的File's Owner,并在弹出窗口中选择“buttonPressed:”。以上拖拽动作称为“连接”,将我们在类控制器中声明的IBOutlet和IBAction连接到IB视图中控件或方法。


下来回到Xcode实现控制器类:

1//
2// HelloiPhoneViewController.m
3// HelloiPhone
4//
5// Created by Elf Sundae on 11/4/10.
6// Copyright 2010 Elf.Sundae(at)Gmail.com. All rights reserved.
7//
8
9#import "HelloiPhoneViewController.h"
10
11@implementation HelloiPhoneViewController
12
13@synthesize textField;
14
15- (IBAction) buttonPressed: (id)sender
16{
17 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello,iPhone!" 
18 message:self.textField.text 
19delegate:nil 
20 cancelButtonTitle:@"OK!" 
21 otherButtonTitles:nil];
22 [alert show];
23 [alert release];
24}
25
26- (void)didReceiveMemoryWarning {
27// Releases the view if it doesn't have a superview.
28 [super didReceiveMemoryWarning];
29 
30// Release any cached data, images, etc that aren't in use.
31}
32
33- (void)viewDidUnload {
34// Release any retained subviews of the main view.
35// e.g. self.myOutlet = nil;
36 self.textField = nil;
37}
38
39
40- (void)dealloc {
41 [textField release];
42 [super dealloc];
43}
44
45@end
复制代码
复制代码

 

运行iPhone模拟器测试后发现键盘不能自动关闭。下面来解决这个问题,让输入完后按键盘上的return即可关闭,或者触摸背景关闭键盘(数字键盘上没有return键,iphone程序常设计为触摸背景关闭键盘输入状态)。

 

完成输入后关闭键盘
当用户按下return键时,将生成一个Did End On Exit事件,此时我们需要让textField取消控件已关闭键盘。
用户当前正在与之交互的控件称为第一响应者(firstResponder)。
方法 - (BOOL)resignFirstResponder 使触发此操作的控件取消第一响应者状态。

我们在控制器类头文件中声明方法:

- (IBAction) textFieldDoneEditing: (id)sender;

保存项目后在IB中将textField的Did End On Exit事件连接到此方法。保存IB返回Xcode实现此方法:

- (IBAction) textFieldDoneEditing: (id)sender
{
//消第一响应者状态
[sender resignFirstResponder];
}
复制代码

 

重新编译并运行,发现可以通过按下return键来完成输入了!

 
通过触摸背景关闭键盘
并非所有键盘布局都有return键,例如数字键盘。苹果公司的iPhone程序是这样做的:在大多数有textField的情况下,在试图中任何无活动控件的位置按下手指都可让键盘消失。实现此功能非常简单,只要创建一个不可见的按钮,将其置于其他所有元素的后面,用于通知textField在检测到触摸操作时生成第一响应者状态。

// 通过触摸屏幕关闭键盘
- (IBAction) backgroundTap: (id)sender
{
// 在非第一响应者控件上调用resignFirstResponder是绝对安全的。
// 因此可以放心的对所有textField调用resignFirstResponder
[textField resignFirstResponder];
}
复制代码

保存代码来到IB。我们现在需要更改nib试图的底层类。
在IB主窗口中单击选择View,command+4 调出身份检查器。将标有Class的字段由UIView更改为UIControl。能够触发操作方法的所有控件都是UIControl的子类,所以通过更改底层类,此View将能够触发操作方法。
现在按command + 2 应该能看到Event。将Touch Down事件连接到File's Owner的backgroundTap:操作。
保存IB返回Xcode后编译并运行,可以看到触摸视图中没有活动控件的任何位置(屏幕背景或lable等控件)都将触发backgroundTap:操作。

 

应用程序图标

应用程序图标为57×57都PNG文件,iPhone将自动将边缘圆角化并让它具有玻璃质感。默认情况下,SDK会搜寻名为icon.png的资源并使用它作为应用程序图标。如果你都图标文件不叫icon.png,可以在plist中添加图标属性,方法为:将图标文件添加到项目Resources文件夹中,打开项目-Info.plist,在Icon file中填入图标文件名。

代码整理

 

HelloiPhoneViewController.h
1 //
2  // HelloiPhoneViewController.h
3  // HelloiPhone
4  //
5 // Created by Elf Sundae on 11/4/10.
6 // Copyright 2010 Elf.Sundae(at)Gmail.com. All rights reserved.
7 //
8 
9 #import <UIKit/UIKit.h>
10 
11 @interface HelloiPhoneViewController : UIViewController
12 {
13 UITextField * textField;
14 }
15 
16 @property (retain, nonatomic) IBOutlet UITextField *textField;
17 
18 - (IBAction) buttonPressed: (id)sender;
19 - (IBAction) textFieldDoneEditing: (id)sender;
20 - (IBAction) backgroundTap: (id)sender;
21 
22 @end
复制代码
HelloiPhoneViewController.m
1 //
2 // HelloiPhoneViewController.m
3 // HelloiPhone
4 //
5 // Created by Elf Sundae on 11/4/10.
6 // Copyright 2010 Elf.Sundae(at)Gmail.com. All rights reserved.
7 //
8 
9 #import "HelloiPhoneViewController.h"
10 
11 @implementation HelloiPhoneViewController
12 
13 @synthesize textField;
14 
15 - (IBAction) buttonPressed: (id)sender
16 {
17 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello,iPhone!" 
18 message:self.textField.text 
19 delegate:nil 
20 cancelButtonTitle:@"OK!" 
21 otherButtonTitles:nil];
22 [alert show];
23 [alert release];
24 }
25 
26 - (IBAction) textFieldDoneEditing: (id)sender
27 {
28 // 取消textField第一响应者状态
29 [sender resignFirstResponder];
30 }
31 
32 // 通过触摸屏幕关闭键盘
33 - (IBAction) backgroundTap: (id)sender
34 {
35 // 在非第一响应者控件上调用resignFirstResponder是绝对安全的。
36 // 因此可以放心的对所有textField调用resignFirstResponder
37 [textField resignFirstResponder];
38 }
39 - (void)didReceiveMemoryWarning {
40 // Releases the view if it doesn't have a superview.
41 [super didReceiveMemoryWarning];
42 
43 // Release any cached data, images, etc that aren't in use.
44 }
45 
46 - (void)viewDidUnload {
47 // Release any retained subviews of the main view.
48 // e.g. self.myOutlet = nil;
49 self.textField = nil;
50 }
51 
52 
53 - (void)dealloc {
54 [textField release];
55 [super dealloc];
56 }
57 
58 @end
复制代码

 


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 擤鼻涕耳朵堵了怎么办 脸用什么都过敏怎么办 1岁宝宝流清鼻涕怎么办 2岁宝宝流清鼻涕怎么办 3岁儿童流清鼻涕怎么办 鼻子老是流清水鼻涕怎么办 5岁儿童感冒鼻塞怎么办 3岁宝宝感冒鼻塞怎么办 宝宝9个月流鼻涕怎么办 鼻涕又黄又粘稠怎么办 鼻子一直流黄水怎么办 宝宝眼屎多又黄怎么办 宝宝痰多鼻涕多怎么办 小孩痰多鼻涕多怎么办 用qq登不了微博怎么办 中国银行u盾丢了怎么办 我的世界遇见him怎么办 考军校年龄超了怎么办 dnf组队就红电脑怎么办 九阳高压锅漏气怎么办 晋江买了防盗章怎么办 开车撞了人应该怎么办 开车撞了人没钱怎么办 驾照扣分12分后怎么办 车被交警拖走了怎么办 符石耐久没了怎么办 冒险岛2老是掉线怎么办 冒险岛老是掉线怎么办 冒险岛2延迟高怎么办 高速超速扣6分怎么办 优酷视频有密码怎么办 斗鱼直播很卡怎么办 鼠标的滑轮坏了怎么办 宝马1系烧机油怎么办 原房主不迁户口怎么办 做假账被发现了怎么办 裆部潮湿有异味怎么办 用了开塞露还是拉不出大便怎么办 安卓模拟器很卡怎么办 锁屏图案忘了怎么办 手机图案锁忘了怎么办