行书千字文字帖全文:Ipad开发课程-Charpter3 窗口工具 ? 疯狂的老九

来源:百度文库 编辑:九乡新闻网 时间:2024/04/27 13:25:08

Ipad开发课程-Charpter3 窗口工具

前面我们已经熟悉了一些窗口工具,如Round Rect Button,TestField,和Label。虽然很简单,但是他们给了你一个很好的机会了解这些应用后面的outlets和actions原理。

我们可以从Interface builder的Library窗口中看到更多的组件:

Library窗口中的组件有几种分类:

控制类:用户窗口的控制,如:View Controller,Tab Bar controller,Navigation Controller和其它。

数据类:用于数据的显示,如Image View,Table View,Data Picker,Picker View和其它

输入和值:用于用户输入数值,如:Label,Round Rect Button,Tect Field和其它

窗口,标签、和工具栏:提供多种窗口、工具栏的显示,如:View,Search Bar,Toolbar和其它

在接下来的章节,我们将会学习怎么样在Library中使用这些组件。尽管我们不能将每一个组件一一讲解,但是我们会在我们的实例中使用一些具有代表性的组件,以后你自己也能根据这些学会使用其它组件。

使用Alert(报警)窗口

UIAlertView类没有在Libray里面显示,UIAlertView类负责在程序运行过程中创建一个警报窗口。因此我们必须用代码来实现。

我们所设计的iPad程序会对使用者在操作程序的时候发送消息,在这个过程中UIAlertView我们会在程序中经常的用到。除此之外,它还能用于程序运行过程中故障的排除和显示变量的数值。

来,让我们试试。Alert窗口的使用:代码下载:UsingViews.rar

1.新建项目,取名:UsingViews

2.编辑UsingViewsViewController.m文件,添加下面代码到viewDidLoad方法中:

- (void)viewDidLoad {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“Hello” message:@“This is an alert view” delegate:self 

cancelButtonTitle:@“OK” otherButtonTitles:nil];

[alert show];

[alert release];

[super viewDidLoad];

}

3.Ctrl+R运行,在模拟器中我们可以看到警告窗口被显示出来了。————→

4.接着我们继续修改这段代码中的otherButtonTitles参数,为alert窗口添加两个选项:

 

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@“Hello”message:@“This is an alert view”delegate:selfcancelButtonTitle:@“OK”otherButtonTitles:@“Option 1”, @“Option 2”, nil];

 

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“Hello” message:@“This is an alert view” delegate:self 

cancelButtonTitle:@“OK”

otherButtonTitles:@“Option 1”,@“Option 2”, nil];

 

 

5.继续在UsingViewsViewContoller.h文件中添加红色段代码

 

#import

@interface UsingViewsViewController : UIViewController

{

}

@end

 

6.在UsingViewsViewChontroller.m文件中添加方法:

- (void)alertView:(UIAlertView *)alertView

clickedButtonAtIndex:(NSInteger)buttonIndex {

NSLog([NSString stringWithFormat:@“%d”, buttonIndex]);

7.运行,我们可以看到除了ok外,多了2个选项。

8.点击其中的按钮,看其结果。

9.在代码编辑窗口,我们用Ctrl+Shift+R运行调试控制台(Debugger Consle)。观察输出值。每次我们点击不同的按钮,它就返回不一样的数值:

OK按钮返回0

Option1按钮返回1

Option2按钮返回2

这个的运行原理是怎么样的呢?

要使用UIAlertView,我们首先要通过多个参数来定义和创建它,这段代码就是创建和定义它的:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“Hello” message:@“This is an alert view” delegate:self cancelButtonTitle:@“OK” 

otherButtonTitles:nil];

第一个参数是设置警报窗口的Title,我们设置为“Hello”。第二个参数是message:我们设置为“This is analert”。第三个参数是delegate,我们需要设立一个对象,将UIAlertView对象具体化,在这个例子中我们设为self,它的意思是这个事件处理程序将在当前阶段被应用,这就是图形控制器。cancelButtonTitle参数将显示一个按钮去关闭Alter窗口。最后,otherButtonTitles参数允许我们设置显示添加多个按钮用于返回不同的值,如果不需要增加按钮,我们设置它为nil。

要显示alter 窗口,我们需要用到show方法

[alert show];

如果你需要几个按钮,那我们就需要设置otherButtonTitles参数:

otherButtonTitles:@“Option 1”,@“Option 2”, nil];

注意,在这段定义最后,我们也需要设置参数nil,不然程序会执行出错的.

现在我们有了3个按钮,不然用户按哪个按钮,程序都应该做出相应的反馈.要实现这样的功能我们还需要用到UIAlertView类.我们需要添加UIAlertViewDelegate协议来协助视图控制器.

@interface UsingViewsViewController : UIViewController

{

//...

这个UIAlertViewDelegate协议包含了很多关联alert视窗的方法.要知道用户按钮后的反应,我们还需要对alertViewclickedButtonAtIndex进行定义方法:

 

- (void)alertView:(UIAlertView *)alertView

clickedButtonAtIndex:(NSInteger)buttonIndex {

NSLog([NSString stringWithFormat:@“%d”, buttonIndex]);

按钮点击后的值将会通过clickedButtonAtIndex参数来显示.