在IOS的用户接口向导中,苹果提供了另外一种显示警告框的手法,叫做UIActionSheet.它和UIAlertView比起来不会显得过于急切和紧张。而是很温和地在继续流程之前给用户提供了诸多选择。
1.普通的sheet框使用
同UIAlertView一样,sheet也可以很简单的创建并且显示.
- (IBAction)actionSheetShow:(id)sender {
//destructiveButton 的颜色和其他按钮不同,呈现红色
//表示用户需要注意,一般用于不可逆操作
UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:@"Hello" delegate:self cancelButtonTitle:@"OK" destructiveButtonTitle:@"此项需要用户注意" otherButtonTitles: nil];
[sheet showInView:self.view];
}
注意:如果我们开发了一款基于多标签页的程序(UITabbar),在每个标签页中显示sheet框的方法会发生不同。这时要调用“showFromTabBar”的方式来代替“showInView:aView”,否则界面的显示层次会发生问题。同样的,如果视图的底部有一条工具栏,我们可以调用“showFromToolbar”的方法来显示sheet框而不是“shwoInView:aView”.
如果显示的选择项过多,会出现滚动条选项在actionSheet
之后,同样可以使用代理方法获取用户的选择
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
id buttonSheet=[actionSheet buttonTitleAtIndex:buttonIndex];
NSLog(@"按下了%@按钮",buttonSheet);
}
显示结果如下:
2.含进度条的sheet框
我们介绍进度条的sheet框,不显示任何按钮。同样借用模态的特性达到开发的某些目的。
-(void)showProgressOnView:(UIView *)aView
{
if(!aView)
{
return;
} //sheet的创建
UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:@"这时进度条\n\n\n" delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil]; //进度条创建
UIProgressView *progress=[[UIProgressView alloc]initWithFrame:CGRectMake(0.0f, 40.0f, 220.0f, 90.0f)]; //进度条配置
progress.progressViewStyle = UIProgressViewStyleDefault;
progress.progress=0.0f;
[sheet addSubview:progress]; //起一个定时器,来更新进度条
[NSTimer scheduledTimerWithTimeInterval:0.03f target:self selector:@selector(updateProgress:) userInfo:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:progress,sheet, nil] forKeys:[NSArray arrayWithObjects:@"progress",@"sheet", nil]] repeats:YES]; [sheet showInView:self.view];
progress.center=CGPointMake(CGRectGetWidth(sheet.bounds)/, CGRectGetHeight(sheet.bounds)/);
}
NSTimer的响应函数如下:
//NSTimer的响应函数如下
-(void)updateProgress:(NSTimer *)aTimer
{
UIProgressView *progress = nil;
UIActionSheet *sheet =nil; //将定时器中有用的内容取出来
NSDictionary *dicInfo =aTimer.userInfo;
if(!dicInfo)
{
return;
} progress = [dicInfo objectForKey:@"progress"];
if(!progress)
{
return;
} //进度条满了
if(progress.progress>=3.0f)
{
sheet = [dicInfo objectForKey:@"sheet"];
//将sheet关闭
if(sheet)
{
[sheet dismissWithClickedButtonIndex: animated:YES];
}
//定时器销毁
[aTimer invalidate]; }
else
{
//进度条的速度
progress.progress+=0.01f;
}
}
显示效果如下:
3.含自定义控件的sheet框
sheet框是一个UIView的子类,因而他不仅能够提供按钮选项,一定也支持往他身上加其他功能的控件。
这次让我们加一个取值器玩一玩,比如取值器中有5个待取的城市:上海,北京,深圳,香港,天津。具体实现代码如下:
-(void)showCustomControlType:(UIView *)aView
{
//创建sheet,为picker腾出空间
UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"OK", nil];
sheet.actionSheetStyle=UIActionSheetStyleBlackOpaque; UIPickerView *picker=[[UIPickerView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 216.0f)];
picker.delegate=self;
picker.dataSource=self;
picker.showsSelectionIndicator=YES; [sheet addSubview:picker];
[sheet showInView:aView];
} -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch (row) {
case :
return @"上海";
break;
case :
return @"北京";
break;
case :
return @"深圳";
break;
case :
return @"香港";
break;
case :
return @"天津";
break;
default:
return @"上海";
break;
}
} -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return ;
} -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return ;
}
运行结果如下: