行书偏旁部首:Ipad开发课程-Charpter3 代码实现窗口 ? 疯狂的老九
来源:百度文库 编辑:九乡新闻网 时间:2024/04/27 19:01:42
Ipad开发课程-Charpter3 代码实现窗口
15Aug
Posted by: 9 in: Iphone程序开发
到了这里,你应该可以通过拖来拖去搞定很多应用程序了,但是有些简单程序可以拖来拖去,高级的就不行了,呵呵不然满大街都是app程序了,还卖钱?送人估计都不要!有些时候我们需要一个动态的界面,比如游戏.
interface Builder可能比较容易,但是也容易让一些人脑袋晕忽忽的.因为我们经常有很多方法通过界面模式来创建一个程序,但是有时候这样有很多意想不到的结果.还有一些开发者发誓用代码写程序.
来,我们来用代码实现View窗口DynamicViews.rar
1.打开Xcode,建立一个View-based应用项目,DynamicViews
2.进入DynamicViewsViewController.m文件,编代码:
#import “DynamicViewsViewController.h”@implementation DynamicViewsViewController- (void)loadView {
#import “DynamicViewsViewController.h”
@implementation DynamicViewsViewController
- (void)loadView {
//---create a UIView object---
UIView *view =
[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
view.backgroundColor = [UIColor lightGrayColor];
//---create a Label view---
CGRect frame = CGRectMake(10, 15, 300, 20);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:@“Verdana” size:20];
label.text = @“This is a label”;
label.tag = 1000;
//---create a Button view---
frame = CGRectMake(10, 70, 300, 50);
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setTitle:@“Click Me, Please!” forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
button.tag = 2000;
[button addTarget:self
action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
[view addSubview:label];
[view addSubview:button];
self.view = view;
[label release];
}
-(IBAction) buttonClicked: (id) sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“Action invoked!”
message:@“Button clicked!” delegate:self
cancelButtonTitle:@“OK”
otherButtonTitles:nil];
[alert show];
[alert release];
}
@end
3.Ctrl+R,我们用代码实现了一个Label标签和一个按钮控件,点击按钮显示一个alert窗口显示一个消息.
它是怎么实现的呢?
你可以用loadView方法来创建你的程序窗口,这个方法只有在你运行程序的时候才会出现.
我们首先要使用的UIView对象,它能让我们创建一个或几个窗口:
//---create a UIView object---
UIView *view =[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
//---set the background color to lightgray---
view.backgroundColor = [UIColor lightGrayColor];
接下来,我们在窗口中创建Label标签和设置它的显示:
//---create a Label view---
CGRect frame = CGRectMake(10, 15, 300, 20);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:@“Verdana” size:20];
label.text = @“This is a label”;
label.tag = 1000;
tag标清是必须要设置的,这样在多窗口实现的时候我们才知道是哪个在运行.
我们再用buttonWithType:方法创建一个按钮,定义UIButtonTypeRoundedRect常量.这个方法将返回一个UIRoundedRectButton对象的值(这个是属于UIButton的之类).
//---create a Button view---
frame = CGRectMake(10, 70, 300, 50);
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setTitle:@“Click Me, Please!” forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
button.tag = 2000;
定义buttonClicked:方法,实现按钮被敲击的返回值;
[button AddTarget:Self action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
定义label和button视窗到早先我们创建的View视窗:
[view addSubview:label];
[view addSubview:button];
最后我们要对View对象赋值
self.view = view;
理解View窗口的层次结构
当一个窗口被创建或者添加的时候,应该是一个树形结构.如果要区别他们,我们需要修改UIButton对象:
//---create a Button view---
frame = CGRectMake(10, 30, 300, 50);
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setTitle:@“Click Me, Please!”
forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
button.tag = 2000;
[button addTarget:self
action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
当你现在运行程序的时候,你可以注意到按钮将label标签遮住了.
通过这个代码实现的两个控件显示:
[view addSubview:label];
[view addSubview:button];
在这两个控件被添加后,我们可以用恶心changeSubviewAtIndex:withSubviewAtIndex:方法交换控件的显示顺序:
[self.view addSubview:label];
[self.view addSubview:button];
[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
[label release];
再次运行这个程序,我们可以看到两个标签显示顺序已经变换:
要定义每个已添加控件的顺序,你能通过定义tag值来确定:
[self.view addSubview:label];
[self.view addSubview:button];
[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
for (int i=0; i<[self.view.subviews count]; ++i) {
UIView *view = [self.view.subviews objectAtIndex:i];
NSLog([NSString stringWithFormat:@“%d”, view.tag]);
}
接下来用UIView对象采用递归的方法进行定义:
-(void) printViews: (UIView *) view {
if ([view.subviews count] > 0){
for (int i=0; i<[view.subviews count]; ++i) {
UIView *v = [view.subviews objectAtIndex:i];
NSLog([NSString stringWithFormat:@“View index: %d Tag: %d”,i, v.tag]);
[self printViews:v];
}
} else
return;
}
想要对已经定义好的窗口中除以一个窗口,我们会用到removeFromSuperview方法.例如我们将label标签层除移:
[label removeFromSuperview];
本小节完!