作品川西早牧--聚焦瞬间

2010_08_03_9999_40-w.jpg

2010_08_03_9999_48-d-w.jpg

2010_08_03_9999_60-w.jpg

2010_08_03_9999_64-w.jpg

2010_08_03_9999_66-w.jpg

2010_08_03_9999_78-w.jpg

2010_08_03_9999_85-w.jpg

2010_08_04_9999_210_1_2_w.jpg

到了这里,你应该可以通过拖来拖去搞定很多应用程序了,但是有些简单程序可以拖来拖去,高级的就不行了,呵呵不然满大街都是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窗口显示一个消息.

3-20.jpg

它是怎么实现的呢?

你可以用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];

 

3-21.jpg

在这两个控件被添加后,我们可以用恶心changeSubviewAtIndex:withSubviewAtIndex:方法交换控件的显示顺序:

 

[self.view addSubview:label];

[self.view addSubview:button];

[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];

[label release];

再次运行这个程序,我们可以看到两个标签显示顺序已经变换:

3-22.jpg

要定义每个已添加控件的顺序,你能通过定义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];

本小节完!

 

 

 

 

 

 

 

 

 

 

Web应用视窗

要在你的应用程序你们读取网页页面,或者内镶Web浏览器我们就需要用UIWebView控件。使用Web视窗,你就可以发送一个读取Web链接的需求,在我们在本地应用中读取Web应用十分有用.你需要做的就是讲HTML页面载入你的资源文件夹,然后在Web视窗中在运行的时候读取它们.

根据你的Web应用程序的复杂程度,比如链接服务端web程序,使用到CGI,PHP或者其它程序,你可能需要做一些额外的开发.

实例:用Web控件读取网页,代码下载:UsingViews2.rar

1.打开Xcode,创建一个新View-based应用项目,取名UsingViews2.

2.双击UsingViews2ViewController.xib文件.

3.在View窗口,从Library里面拉动一个Web控件到View窗口,在Attributes Inspector窗口选择Scales Page to Fit,效果如下图

3-18.jpg

4.在UsingViews2ViewControllercontroller.h文件,为Web控件添加一个outlet声明.

 

#import <UIKit/UIKit.h>
@interface UsingViews2ViewController : UIViewController {
IBOutlet UIWebView *webView;
}
@property (nonatomic, retain) UIWebView *webView;
@end

 

#import <UIKit/UIKit.h>

@interface UsingViews2ViewController : UIViewController {

IBOutlet UIWebView *webView;

}

@property (nonatomic, retain) UIWebView *webView;

@end

5.在Interface Builder,连接这个UIWebView的outlet到Web控件.

6.进入UsingViews2ViewController.m文件,添加下面颜色字体代码:

#import “UsingViews2ViewController.h”

 

@implementation UsingViews2ViewController

@synthesize webView;

- (void)viewDidLoad {

NSURL *url = [NSURL URLWithString:@“http://www.apple.com”];

NSURLRequest *req = [NSURLRequest requestWithURL:url];

[webView loadRequest:req];

[super viewDidLoad];

}

- (void)dealloc {

[webView release];

[super dealloc];

}

7.Ctrl+R,试试你写的浏览器吧

3-19.jpg

程序原理:

用Web控件读取一个URL链接,你首先要使用URLWithString方法通过URL实例化一个NSURL对象:

语句:NSURL *url = [NSURL URLWithString:@“http://www.apple.com”];

然后我们再通过NSURL项目创建一个NSURLRequest项目,那就是requestWithURL方法:

NSURLRequest *req = [NSURLRequest requestWithURL:url];

最后,我们通过loadRequest方法来使NSURLRequest对象读取Web视窗

[webView loadRequest:req];

just ok.

 

 

 

聚焦瞬间

0140a_41a_39a_w.jpg

0144a_3a_2a_w.jpg

0146a_7a_5a_w.jpg

2010_08_02_9999_18-w.jpg

0156a-w.jpg

0158a-w.jpg

2010_07_31_9999_3_1_2_w.jpg

2010_08_01_9999_15-w.jpg

2010_08_02_9999_23-w.jpg

2010_08_02_9999_174-w.jpg

2010_08_02_9999_198-w.jpg

全国哀悼日,舟曲祈福

还有最重要的就是为表达对舟曲人民的不幸,我在此以沉重的心情为舟曲人民哀悼.
希望你们以后快点回到以往,回到以前幸福的生活中


舟曲泥石流已经造成1156人遇难 588人失踪
希望死者走好,生还者以后可以活得更加好


还有,就是向解放军等等人致敬,是你们,总是第一时间出现在受灾的群众中.致敬!

About Me

我是一个超级苹果迷+老乔的fans和Google的簇拥,一切新奇的事物我都好奇,摄影、运动、旅游、读书、电影、游戏都是我的爱好,荒淫大家给我留言,发邮件,请我吃饭、带我旅游(也不要忘记了我的领导同志!)……

« 2012年2月 »
   1234
567891011
12131415161718
19202122232425
26272829   

微博

统计

文章:119篇
评论:95条 (0条Spam)
相册:11个 (339张图片)
主题:InSense theme