UIActionSheet详解

来源:互联网 发布:天猫魔盒直播软件 编辑:程序博客网 时间:2024/04/30 08:57

转载自:http://blog.csdn.net/flyter/article/details/7532687

UIActionSheet是iOS开发中实现警告框的重要的类,在很多情况下都要用到,先来一睹其芳容:

 



实现步骤如下:

一、为了让控制器类充当操作表的委托,控制器类需要遵从UIActionSheetDelegate协议。

[plain] view plaincopy
  1. @interface UIActionSheetDemoViewController : UIViewController <UIActionSheetDelegate>{  
  2.   
  3. }  

二、生成UIActionSheet并显示。
[plain] view plaincopy
  1. UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Are you sure?"  
  2.                                                          delegate:self   
  3.                                                 cancelButtonTitle:@"No way!"   
  4.                                            destructiveButtonTitle:@"Yes, I'm sure."   
  5.                                                 otherButtonTitles:@"Button One", @"Button Two", nil];  
  6. [actionSheet showInView:self.view];  
  7. [actionSheet release];  

三、点击按钮后的事件。
[plain] view plaincopy
  1. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {  
  2.     NSLog(@"%i", buttonIndex);  
  3.     if (buttonIndex == actionSheet.cancelButtonIndex) {  
  4.         return;  
  5.     }  
  6.     switch (buttonIndex) {  
  7.         case 0: {  
  8.             NSLog(@"Item 1 Selected");  
  9.             break;  
  10.         }  
  11.         case 1: {  
  12.             NSLog(@"Item 2 Selected");  
  13.             break;  
  14.         }  
  15.         case 2: {  
  16.             NSLog(@"Item 3 Selected");  
  17.             break;  
  18.         }  
  19.     }  
  20. }  

代码样例请下载:http://download.csdn.net/detail/flyter/4274695 

 

 

下面说说如何动态地添加UIActionSheet按钮。

 

一、UIActionSheet的通常实现方法:

[plain] view plaincopy
  1. - (void)testActionSheetStatic {  
  2.     UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Static UIActionSheet"  
  3.                                                        delegate:self  
  4.                                               cancelButtonTitle:@"Cancel"  
  5.                                          destructiveButtonTitle:nil  
  6.                                               otherButtonTitles:@"Item A", @"Item B", @"Item C", nil];  
  7.     [sheet showFromRect:view.bounds inView:view animated:YES];  
  8.     [sheet release];  
  9. }  

 

 

二、 如果事先知道各个按钮并且再也不会改变的情况下,这样的实现是OK的。但如果我要在运行时改变应该怎么办呢?动态添加按钮看起来应该也很简单,不要init函数中指定而在之后添加可以了,如下代码就展示了这点。

[plain] view plaincopy
  1. - (void)testActionSheetDynamic {  
  2.     // 创建时仅指定取消按钮  
  3.     UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Dynamic UIActionSheet"  
  4.                                                        delegate:self  
  5.                                               cancelButtonTitle:@"Cancel"  
  6.                                          destructiveButtonTitle:nil  
  7.                                               otherButtonTitles:nil];  
  8.     // 逐个添加按钮(比如可以是数组循环)  
  9.     [sheet addButtonWithTitle:@"Item A"];  
  10.     [sheet addButtonWithTitle:@"Item B"];  
  11.     [sheet addButtonWithTitle:@"Item C"];  
  12.     [sheet showFromRect:view.bounds inView:view animated:YES];  
  13.     [sheet release];  
  14. }  


 

        运行下就发现问题很明显——取消按钮是在视图的顶部,而标准做法是显示在底部。怎么解决呢?如果在init函数中添加取消按钮就无法解决了。最后找到了一种将取消按钮也动态添加。

[plain] view plaincopy
  1. - (void)testActionSheetDynamic {  
  2.     // 创建时不指定按钮  
  3.     UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Dynamic UIActionSheet"  
  4.                                                        delegate:self  
  5.                                               cancelButtonTitle:nil  
  6.                                          destructiveButtonTitle:nil  
  7.                                               otherButtonTitles:nil];  
  8.     // 逐个添加按钮(比如可以是数组循环)  
  9.     [sheet addButtonWithTitle:@"Item A"];  
  10.     [sheet addButtonWithTitle:@"Item B"];  
  11.     [sheet addButtonWithTitle:@"Item C"];  
  12.   
  13.     // 同时添加一个取消按钮  
  14.     [sheet addButtonWithTitle:@"Cancel"];  
  15.     // 将取消按钮的index设置成我们刚添加的那个按钮,这样在delegate中就可以知道是那个按钮  
  16.      sheet.cancelButtonIndex = sheet.numberOfButtons-1;  
  17.     [sheet showFromRect:view.bounds inView:view animated:YES];  
  18.     [sheet release];  
  19. }  


 

        这样取消按钮就显示在底部并且行为也符合预期了。

        对我来说现在剩下的最大一个疑问就是destructive按钮到底是什么(Apple文档也没有清晰地说明这点)?一些实验结果也表明它实际上和 取消按钮并无区别,只不过它有一个红色背景而不是黑色的。所有如果在上例中改变destructiveButtonIndex而不是 cancelButtonIndex,就可以看到标有“取消”的按钮有红色背景了。

 

三、出于完整性的考虑,和上述代码相匹配的delegate代码如下:

[plain] view plaincopy
  1. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex  
  2. {  
  3.     if (buttonIndex == actionSheet.cancelButtonIndex) {  
  4.         return;  
  5.     }  
  6.     switch (buttonIndex)  
  7.     {  
  8.         case 0: {  
  9.             NSLog(@"Item A Selected");  
  10.             break;  
  11.         }  
  12.         case 1: {  
  13.             NSLog(@"Item B Selected");  
  14.             break;  
  15.         }  
  16.         case 2: {  
  17.             NSLog(@"Item C Selected");  
  18.             break;  
  19.         }  
  20.     }  
  21. }  


 

最后说说如何自定义一个UIActionSheet类。

 

一、自定义CustomActionSheet类。

CustomActionSheet类继承UIActionSheet,具体的实现如下所示:

(1)CustomActionSheet.h头文件:

[plain] view plaincopy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface CustomActionSheet : UIActionSheet {  
  4.     UIToolbar* toolBar;  
  5.     UIView* view;  
  6. }  
  7.   
  8. @property(nonatomic,retain)UIView* view;  
  9. @property(nonatomic,retain)UIToolbar* toolBar;  
  10.   
  11. -(id)initWithHeight:(float)height WithSheetTitle:(NSString*)title;  
  12.   
  13. @end  


(2)CustomActionSheet.m实现文件:

[plain] view plaincopy
  1. #import "CustomActionSheet.h"  
  2.   
  3. @implementation CustomActionSheet  
  4.   
  5. @synthesize view;  
  6. @synthesize toolBar;  
  7.   
  8. -(id)initWithHeight:(float)height WithSheetTitle:(NSString*)title{  
  9.     self = [super init];  
  10.     if (self) {  
  11.         int theight = height - 40;  
  12.         int btnnum = theight/50;  
  13.         for(int i=0; i<btnnum; i++){  
  14.             [self addButtonWithTitle:@" "];  
  15.         }  
  16.         toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];  
  17.         toolBar.barStyle = UIBarStyleBlackOpaque;  
  18.         UIBarButtonItem *titleButton = [[UIBarButtonItem alloc] initWithTitle:title   
  19.                                                                         style:UIBarButtonItemStylePlain   
  20.                                                                        target:nil   
  21.                                                                        action:nil];  
  22.           
  23.         UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"   
  24.                                                                         style:UIBarButtonItemStyleDone   
  25.                                                                        target:self  
  26.                                                                        action:@selector(done)];  
  27.   
  28.         UIBarButtonItem *leftButton  = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"   
  29.                                                                         style:UIBarButtonItemStyleBordered   
  30.                                                                        target:self   
  31.                                                                        action:@selector(docancel)];  
  32.   
  33.         UIBarButtonItem *fixedButton  =   
  34.             [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace   
  35.                                                           target:nil   
  36.                                                           action:nil];  
  37.           
  38.         NSArray *array =   
  39.             [[NSArray alloc] initWithObjects:leftButton,fixedButton,titleButton,fixedButton,rightButton,nil];  
  40.         [toolBar setItems: array];  
  41.         [titleButton release];  
  42.         [leftButton release];  
  43.         [rightButton release];  
  44.         [fixedButton release];  
  45.         [array release];  
  46.         [self addSubview:toolBar];  
  47.         view = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, height-44)];  
  48.         view.backgroundColor = [UIColor groupTableViewBackgroundColor];  
  49.         [self addSubview:view];  
  50.     }  
  51.     return self;  
  52. }  
  53.   
  54. -(void)done{  
  55.     [self dismissWithClickedButtonIndex:0 animated:YES];  
  56. }  
  57.   
  58. -(void)docancel{  
  59.     [self dismissWithClickedButtonIndex:0 animated:YES];  
  60. }  
  61.   
  62. -(void)dealloc{  
  63.     [view release];  
  64.     [super dealloc];  
  65. }  
  66.   
  67. @end  



二、利用自定义的CustomActionSheet类显示提示框。

[plain] view plaincopy
  1. -(IBAction)doClick:(id)sender{  
  2.     CustomActionSheet* sheet = [[CustomActionSheet alloc] initWithHeight:284.0f   
  3.                                                           WithSheetTitle:@"自定义ActionSheet"];  
  4.     UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0,50, 320, 50)];  
  5.     label.text = @"这里是要自定义放的控制";  
  6.     label.backgroundColor = [UIColor clearColor];  
  7.     label.textAlignment = UITextAlignmentCenter;  
  8.     [sheet.view addSubview:label];  
  9.   
  10.     [sheet showInView:self.view];  
  11.     [sheet release];  
  12. }  


演示:

 

代码样例请下载:http://download.csdn.net/detail/flyter/4274763