iPhone开发之UIActionSheet

本文介绍如何使用iOS开发中的UIActionSheet类实现警告框,并详细阐述了如何动态添加按钮以增强功能。通过实例代码展示了静态与动态添加按钮的方法,包括自定义UIActionSheet类以实现个性化外观和行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



实现步骤如下:

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

  1. @interface UIActionSheetDemoViewController : UIViewController <UIActionSheetDelegate>{ 
  2.  
@interface UIActionSheetDemoViewController : UIViewController <UIActionSheetDelegate>{

}

二、生成UIActionSheet并显示。
  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]; 
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Are you sure?"
                                                         delegate:self 
                                                cancelButtonTitle:@"No way!" 
                                           destructiveButtonTitle:@"Yes, I'm sure." 
                                                otherButtonTitles:@"Button One", @"Button Two", nil];
[actionSheet showInView:self.view];
[actionSheet release];

三、点击按钮后的事件。
  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.     } 
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSLog(@"%i", buttonIndex);
    if (buttonIndex == actionSheet.cancelButtonIndex) {
        return;
    }
    switch (buttonIndex) {
        case 0: {
            NSLog(@"Item 1 Selected");
            break;
        }
        case 1: {
            NSLog(@"Item 2 Selected");
            break;
        }
        case 2: {
            NSLog(@"Item 3 Selected");
            break;
        }
    }
}

代码样例请下载:http://download.youkuaiyun.com/detail/flyter/4274695

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

一、UIActionSheet的通常实现方法:

  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]; 
- (void)testActionSheetStatic {
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Static UIActionSheet"
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                         destructiveButtonTitle:nil
                                              otherButtonTitles:@"Item A", @"Item B", @"Item C", nil];
    [sheet showFromRect:view.bounds inView:view animated:YES];
    [sheet release];
}

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

  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]; 
- (void)testActionSheetDynamic {
    // 创建时仅指定取消按钮
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Dynamic UIActionSheet"
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                         destructiveButtonTitle:nil
                                              otherButtonTitles:nil];
    // 逐个添加按钮(比如可以是数组循环)
    [sheet addButtonWithTitle:@"Item A"];
    [sheet addButtonWithTitle:@"Item B"];
    [sheet addButtonWithTitle:@"Item C"];
    [sheet showFromRect:view.bounds inView:view animated:YES];
    [sheet release];
}


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

  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]; 
- (void)testActionSheetDynamic {
    // 创建时不指定按钮
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Dynamic UIActionSheet"
                                                       delegate:self
                                              cancelButtonTitle:nil
                                         destructiveButtonTitle:nil
                                              otherButtonTitles:nil];
    // 逐个添加按钮(比如可以是数组循环)
    [sheet addButtonWithTitle:@"Item A"];
    [sheet addButtonWithTitle:@"Item B"];
    [sheet addButtonWithTitle:@"Item C"];

    // 同时添加一个取消按钮
    [sheet addButtonWithTitle:@"Cancel"];
    // 将取消按钮的index设置成我们刚添加的那个按钮,这样在delegate中就可以知道是那个按钮
     sheet.cancelButtonIndex = sheet.numberOfButtons-1;
    [sheet showFromRect:view.bounds inView:view animated:YES];
    [sheet release];
}


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

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

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

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


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

一、自定义CustomActionSheet类。

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

(1)CustomActionSheet.h头文件:

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

@interface CustomActionSheet : UIActionSheet {
    UIToolbar* toolBar;
    UIView* view;
}

@property(nonatomic,retain)UIView* view;
@property(nonatomic,retain)UIToolbar* toolBar;

-(id)initWithHeight:(float)height WithSheetTitle:(NSString*)title;

@end


(2)CustomActionSheet.m实现文件:

  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. -(void)done{ 
  54.     [self dismissWithClickedButtonIndex:0 animated:YES]; 
  55.  
  56. -(void)docancel{ 
  57.     [self dismissWithClickedButtonIndex:0 animated:YES]; 
  58.  
  59. -(void)dealloc{ 
  60.     [view release]; 
  61.     [super dealloc]; 
  62.  
  63. @end 
#import "CustomActionSheet.h"

@implementation CustomActionSheet

@synthesize view;
@synthesize toolBar;

-(id)initWithHeight:(float)height WithSheetTitle:(NSString*)title{
    self = [super init];
    if (self) {
        int theight = height - 40;
        int btnnum = theight/50;
        for(int i=0; i<btnnum; i++){
            [self addButtonWithTitle:@" "];
        }
        toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        toolBar.barStyle = UIBarStyleBlackOpaque;
        UIBarButtonItem *titleButton = [[UIBarButtonItem alloc] initWithTitle:title 
                                                                        style:UIBarButtonItemStylePlain 
                                                                       target:nil 
                                                                       action:nil];
        
        UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                                                                        style:UIBarButtonItemStyleDone 
                                                                       target:self
                                                                       action:@selector(done)];

        UIBarButtonItem *leftButton  = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" 
                                                                        style:UIBarButtonItemStyleBordered 
                                                                       target:self 
                                                                       action:@selector(docancel)];

        UIBarButtonItem *fixedButton  = 
            [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                                          target:nil 
                                                          action:nil];
		
        NSArray *array = 
            [[NSArray alloc] initWithObjects:leftButton,fixedButton,titleButton,fixedButton,rightButton,nil];
        [toolBar setItems: array];
        [titleButton release];
        [leftButton release];
        [rightButton release];
        [fixedButton release];
        [array release];
        [self addSubview:toolBar];
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, height-44)];
        view.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self addSubview:view];
    }
    return self;
}

-(void)done{
    [self dismissWithClickedButtonIndex:0 animated:YES];
}

-(void)docancel{
    [self dismissWithClickedButtonIndex:0 animated:YES];
}

-(void)dealloc{
    [view release];
    [super dealloc];
}

@end



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

  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]; 
-(IBAction)doClick:(id)sender{
    CustomActionSheet* sheet = [[CustomActionSheet alloc] initWithHeight:284.0f 
                                                          WithSheetTitle:@"自定义ActionSheet"];
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0,50, 320, 50)];
    label.text = @"这里是要自定义放的控制";
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = UITextAlignmentCenter;
    [sheet.view addSubview:label];

    [sheet showInView:self.view];
    [sheet release];
}


演示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值