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