您的位置:首页 > 移动开发 > Objective-C

Objective-C研究院之第一个iPhone应用程序(十)

2012-08-09 16:41 357 查看

原创文章如需转载请注明:转载自雨松MOMO程序研究院本文链接地址:Objective-C研究院之第一个iPhone应用程序(十)

Objective-C语法系列在之前的文章中一直在介绍基本的语法的相关知识,但是学习语法的目的还是为了走进iPhoneIOS的开发世界。从今以后Objective-C语法文章将不在更新。全力更新IOS游戏开发软件开发系列文章,这篇文章MOMO将带各位盆友们简单介绍iPhone开发的一些基本控件的使用,简单的构建我们第一个iPhone应用程序。各位盆友们我们先预热一下,嘿嘿。

读过我Android系列开发的盆友应该很清楚这个熟悉的界面吧,哇咔咔~~



获取手机屏幕尺寸的方法

view source

1
//得到屏幕的宽和高
2
CGRect rect=[[UIScreen mainScreen] bounds];
3
CGSize size = rect.size;
4
int
screenWidth = size.width;
5
int
screenHeight = size.height;
1.文本框视图
在视图中加入一个文本框,可在框内攥写一些内容,设置字体颜色,位置 ,大小等等。

view source

01
//创建label视图
02
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 30)];
03
//设置显示内容
04
label.text = @
"雨松MOMO的程序世界"
;
05
//设置背景颜色
06
label.backgroundColor = [UIColor blueColor];
07
//设置文字颜色
08
label.textColor = [UIColor whiteColor];
09
//设置显示位置居中
10
label.textAlignment = UITextAlignmentCenter;
11
//设置字体大小
12
label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20];
2.按钮视图
按钮类型为1 为普通按钮,CGrectMake设置按钮的位置与大小,前两个参数设置按钮起始X 与 Y坐标,后两个参数设置按钮的宽度与高度。

这里重点说一下addTarget, 它可以设置按钮的绑定事件,action:设置按钮点击后响应方法,这行代码的意思是点击这个按钮后程序执行方法ButtonPressed这个函数中的代码。

view source

01
//创建按钮
02
button = [UIButton buttonWithType:1];
03
//设置按钮范围
04
button.frame = CGRectMake(0, 40, screenWidth, 30);
05
//设置按钮显示内容
06
[button setTitle:@
"这是一个按钮"

forState:UIControlStateNormal];
07
//设置按钮显示颜色
08
button.backgroundColor = [UIColor blackColor];
09
//设置按钮改变后 绑定响应方法
10
[button addTarget:self action:@selector(ButtonPressed) forControlEvents:UIControlEventTouchUpInside];
点击这个按钮后进入下面这个方法,弹出一个dialog对话框。

view source

01
- (
void
)ButtonPressed
02
{
03
04
//创建对话框
05
UIAlertView * alertA= [[UIAlertView alloc] initWithTitle:@
"我的视图"

message:@
"欢迎一起学习IPHONE开发"

delegate:self cancelButtonTitle:@
"确定"

otherButtonTitles: nil];
06
//添加取消按钮
07
[alertA addButtonWithTitle:@
"取消"
];
08
//将这个UIAlerView 显示出来
09
[alertA show];
10
//objective-C 不像java 有自己的垃圾回收机制 所以我们在编写程序中一定要注意释放内存 从一开始就养成良好习惯
11
[alertA release];
12
13
}




3.进度条视图
和上面button视图的构建差不多,这里设置进度条最大值与最小值,拖动的时候就可以直接得到这个范围之间的数值,同样将拖动事件绑定在valueChangeTest方法中。

view source

01
//创建进度条
02
slider=[[UISlider alloc] initWithFrame:CGRectMake(0,80,screenWidth,30)];
03
//进度条最大值
04
slider.maximumValue=100;
05
//进度条最小值
06
slider.minimumValue=0;
07
//起始点的位置
08
slider.value=20;
09
//设置背景颜色
10
slider.backgroundColor=[UIColor blackColor];
11
//设置进度条改变后 绑定响应方法
12
[slider addTarget:self action:@selector(valueChangeTest) forControlEvents:UIControlEventValueChanged];
拖动进度条后发生改变进入下面方法,[slider vale]可以得到拖动的进度值。

view source

1
- (
void
)valueChangeTest
2
{
3
4
float

value = [slider value];
5
NSLog(@
"进度条已经发生改变:%f"
,value);
6
7
}




4.编辑框视图
非常常见的视图,可以在编辑框中输入信息。前提是用户触摸点击输入框,这时弹出系统软键盘方可输入信息,但是这个输入框不会自动关闭,须要我们在程序中自己调用代码去关闭,稍后介绍如何关闭这个输入框。

view source

01
//创建文字输入框
02
textfield = [[UITextField alloc] initWithFrame:CGRectMake(0,120,screenWidth,50)];
03
04
//默认显示文字
05
textfield.text = @
"这是一个输入框"
;
06
//点击后显示文字
07
textfield.placeholder = @
"请在输入框是输入信息"
;
08
//文字显示位置,这里居左对齐
09
textfield.textAlignment = UITextAlignmentLeft;
10
//默认显示文字颜色
11
textfield.textColor = [UIColor grayColor];
12
//设置输入的字体
13
textfield.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:0] size:17];
14
//设置输入框的类型,3为普通类型
15
textfield.borderStyle = 3;
16
//点击输入框后清楚原始内容
17
textfield.clearsOnBeginEditing = YES;
18
//设置输入框背景颜色
19
textfield.backgroundColor = [UIColor blackColor];




5.图片视图
设置图片在屏幕中显示的位置,当然这个图片文件必需拷贝到工程当中,拷贝方法可以直接将图片用鼠标拖动到Xcode的工程中。

view source

1
//创建图片视图
2
imageview = [[UIImageView alloc] initWithFrame:
3
CGRectMake(100, 200, 120, 120)];
4
5
//设置图片的显示的资源路径
6
[imageview setImage:[UIImage imageNamed:@
"temp.jpg"
]];
6.透明全屏按钮
它的存在就是为了解决输入法出现后无法自动关闭,就是说如果输入法软键盘出现后 ,这时候点击屏幕任意位置关闭输入法,实现的原理是用户点击到了屏幕中设置的透明按钮,调用关闭输入法方法将输入法关闭了而已 .

view source

1
//创建一个隐藏的按钮
2
backgroudButton=[[UIButton alloc] init];
3
//让这个填充整个屏幕
4
backgroudButton.frame = self.view.frame;
5
//添加按钮的响应时间,用来关闭软键盘
6
[backgroudButton addTarget:self action:@selector(ButtonClick) forControlEvents:UIControlEventTouchUpInside];
点击屏幕任意位置,关闭输入法。

view source

1
-(
void
)ButtonClick
2
{
3
// 触摸屏幕人以地方 关闭软键盘
4
[textfield resignFirstResponder];
5
}
这样所有的视图的代码都已经贴上,这些视图实际上是subView,须要将这些subView添加到屏幕的主视图当中。并且为了避免内存出现泄漏,一定要及时的释放这些视图。

view source

01
//将所有对象添加入视图中
02
[self.view addSubview:backgroudButton];
03
[self.view addSubview:label];
04
[self.view addSubview:imageview];
05
[self.view addSubview:button];
06
[self.view addSubview:slider];
07
[self.view addSubview:textfield];
08
09
//释放所有对象
10
[imageview release];
11
[label release];
12
[slider release];
13
[textfield release];
下面给出完整的代码
HelloWorldViewController.h

view source

01
#import <UIKit/UIKit.h>
02
03
@interface HelloWorldViewController : UIViewController
04
{
05
//文本框
06
UILabel * label;
07
//按钮
08
UIButton * button;
09
//进度条
10
UISlider *slider;
11
//输入框
12
UITextField * textfield;
13
//图片视图
14
UIImageView *imageview ;
15
//背景按钮
16
UIButton * backgroudButton;
17
}
18
@end
HelloWorldViewController.m

view source

001
#import "HelloWorldViewController.h"
002
003
@implementation HelloWorldViewController
004
005
- (
void
)didReceiveMemoryWarning
006
{
007
// Releases the view if it doesn't have a superview.
008
[super didReceiveMemoryWarning];
009
010
// Release any cached data, images, etc that aren't in use.
011
}
012
013
#pragma mark - View lifecycle
014
015
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
016
- (
void
)viewDidLoad
017
{
018
[super viewDidLoad];
019
020
//得到屏幕的宽和高
021
CGRect rect=[[UIScreen mainScreen] bounds];
022
CGSize size = rect.size;
023
int

screenWidth = size.width;
024
int

screenHeight = size.height;
025
026
//创建label视图
027
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 30)];
028
//设置显示内容
029
label.text = @
"雨松MOMO的程序世界"
;
030
//设置背景颜色
031
label.backgroundColor = [UIColor blueColor];
032
//设置文字颜色
033
label.textColor = [UIColor whiteColor];
034
//设置显示位置居中
035
label.textAlignment = UITextAlignmentCenter;
036
//设置字体大小
037
label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20];
038
039
//创建按钮
040
button = [UIButton buttonWithType:1];
041
//设置按钮范围
042
button.frame = CGRectMake(0, 40, screenWidth, 30);
043
//设置按钮显示内容
044
[button setTitle:@
"这是一个按钮"

forState:UIControlStateNormal];
045
//设置按钮显示颜色
046
button.backgroundColor = [UIColor blackColor];
047
//设置按钮改变后 绑定响应方法
048
[button addTarget:self action:@selector(ButtonPressed) forControlEvents:UIControlEventTouchUpInside];
049
050
//创建进度条
051
slider=[[UISlider alloc] initWithFrame:CGRectMake(0,80,screenWidth,30)];
052
//进度条最大值
053
slider.maximumValue=100;
054
//进度条最小值
055
slider.minimumValue=0;
056
//起始点的位置
057
slider.value=20;
058
//设置背景颜色
059
slider.backgroundColor=[UIColor blackColor];
060
//设置进度条改变后 绑定响应方法
061
[slider addTarget:self action:@selector(valueChangeTest) forControlEvents:UIControlEventValueChanged];
062
063
//创建文字输入框
064
textfield = [[UITextField alloc] initWithFrame:CGRectMake(0,120,screenWidth,50)];
065
066
//默认显示文字
067
textfield.text = @
"这是一个输入框"
;
068
//点击后显示文字
069
textfield.placeholder = @
"请在输入框是输入信息"
;
070
//文字显示位置,这里居左对齐
071
textfield.textAlignment = UITextAlignmentLeft;
072
//默认显示文字颜色
073
textfield.textColor = [UIColor grayColor];
074
//设置输入的字体
075
textfield.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:0] size:17];
076
//设置输入框的类型,3为普通类型
077
textfield.borderStyle = 3;
078
//点击输入框后清楚原始内容
079
textfield.clearsOnBeginEditing = YES;
080
//设置输入框背景颜色
081
textfield.backgroundColor = [UIColor blackColor];
082
083
//创建图片视图
084
imageview = [[UIImageView alloc] initWithFrame:
085
CGRectMake(100, 200, 120, 120)];
086
087
//设置图片的显示的资源路径
088
[imageview setImage:[UIImage imageNamed:@
"temp.jpg"
]];
089
090
//创建一个隐藏的按钮
091
backgroudButton=[[UIButton alloc] init];
092
//让这个填充整个屏幕
093
backgroudButton.frame = self.view.frame;
094
//添加按钮的响应时间,用来关闭软键盘
095
[backgroudButton addTarget:self action:@selector(ButtonClick) forControlEvents:UIControlEventTouchUpInside];
096
097
//设置整个视图的背景颜色
098
[self.view setBackgroundColor:[UIColor blackColor]];
099
100
//将所有对象添加入视图中
101
[self.view addSubview:backgroudButton];
102
[self.view addSubview:label];
103
[self.view addSubview:imageview];
104
[self.view addSubview:button];
105
[self.view addSubview:slider];
106
[self.view addSubview:textfield];
107
108
//释放所有对象
109
[imageview release];
110
[label release];
111
[slider release];
112
[textfield release];
113
114
}
115
116
- (
void
)ButtonPressed
117
{
118
119
//创建对话框
120
UIAlertView * alertA= [[UIAlertView alloc] initWithTitle:@
"我的视图"

message:@
"欢迎一起学习IPHONE开发"

delegate:self cancelButtonTitle:@
"确定"

otherButtonTitles: nil];
121
//添加取消按钮
122
[alertA addButtonWithTitle:@
"取消"
];
123
//将这个UIAlerView 显示出来
124
[alertA show];
125
//objective-C 不像java 有自己的垃圾回收机制 所以我们在编写程序中一定要注意释放内存 从一开始就养成良好习惯
126
[alertA release];
127
128
}
129
130
- (
void
)valueChangeTest
131
{
132
133
float

value = [slider value];
134
NSLog(@
"进度条已经发生改变:%f"
,value);
135
136
}
137
138
-(
void
)ButtonClick
139
{
140
// 触摸屏幕人以地方 关闭软键盘
141
[textfield resignFirstResponder];
142
}
143
144
- (
void
)viewDidUnload
145
{
146
[super viewDidUnload];
147
// Release any retained subviews of the main view.
148
// e.g. self.myOutlet = nil;
149
}
150
151
- (
BOOL
)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
152
{
153
// Return YES for supported orientations
154
return

(interfaceOrientation == UIInterfaceOrientationPortrait);
155
}
156
157
@end
最后如果你还是觉得我写的不够详细 看的不够爽 不要紧我把源代码的下载地址贴出来 欢迎大家一起讨论学习雨松MOMO希望可以和大家一起进步。
下载地址:http://download.csdn.net/detail/xys289187120/3645156
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: