行书入门:Ipad开发课程-Charpter3 使用action表单 ? 疯狂的老九

来源:百度文库 编辑:九乡新闻网 时间:2024/04/27 15:28:24

Ipad开发课程-Charpter3 使用action表单

使用Action sheet

尽管Alert view能显示多个按钮,但是它主要是用于一些事件发生时的提醒.如果我们需要显示一个可以让用户多个选择的消息,我们就应该使用Action Sheet.通过Action Sheet我们能显示一个按钮合集来让用户选择.

我们通过这段代码来使用Action Sheet:

 

UIActionSheet *action = [[UIActionSheet alloc]initWithTitle:@“Title of Action Sheet”delegate:selfcancelButtonTitle:@“OK”destructiveButtonTitle:@“Delete Message”otherButtonTitles:@“Option 1”, @“Option 2”, nil];[action showInView:self.view];[action release];

 

UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@“Title of Action Sheet”

delegate:self

cancelButtonTitle:@“OK”

destructiveButtonTitle:@“Delete Message”

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

[action showInView:self.view];

[action release];

当这些按钮中的一个被敲击的时候,视图控制器将通过UIActionSheetDelegate协议让Action sheet来处理这个事件:

#import @interface UsingViewsViewController : UIViewController { }

当一个按钮被按下的时候,actionSheet:clickedButtonAtindex:事件将被激活:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog([NSString stringWithFormat:@“%d”, buttonIndex]); }

Ctrl+R运行,我们将看到这样的显示→

注意OK按钮(通过cancelButtonTitle定义的)并没有在显示。每个按钮所

返回的值(buttonIndex)分别为:

Delete Message ——0

Option 1 ——1

Option 2 ——2

当用户点击action sheet范围外面的时候,action sheet将会取消并且buttonIndex返回值变为3.

 有趣的是如果你将cancelButtonTitle:定义为nil,那么在action sheet被关闭的时候buttonIndex的值

会被自动赋值为-1.