代码实现ActionSheet 和 Alert 基本功能

本文介绍iOS开发中ActionSheet与Alert的区别及应用实例。通过创建一个测试项目,演示如何使用这两种对话框,并提供了详细的代码实现。

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

        本来是打算详细研究下ActionSheet的一些属性,上午就把这篇博客写了,结果老师找办点事,就这样耽搁了,但是今天不能没有记录,只是简单记一下,讲到ActionSheet不禁就会想到Alert,他们有什么区别呢?

Action Sheet就像Windows中的 “确定-取消”对话框一样,用于强制用户进行选择。当用户将要进行的操作具有一定危险时,常常使用Action Sheet对用户进行危险提示,这样,用户有机会进行取消操作。

Alert相当于Windows中的Messagebox,跟Action Sheet也是类似的。不同的是,Alert可以只有一个选择项,而Action Sheet却至少要两个选项。


         想要实现功能是点击一个Button,然后弹出一个Alert警告,然后在Alert上点击一个按钮,又出现一个ActionSheet提示操作,在点击ActionSheet一个Button然后又弹出一Alert个警告,功能效果截图

  


1.创建一个新工程叫ActionSheetDemo; File->New->Project ->single View Application -> next 



2.因为用到了ActionSheet和Alert委托方法,把ActionSheet和Alert协议添加上,在声明下两个对象

#import <UIKit/UIKit.h>

@interface ASDViewController : UIViewController<UIAlertViewDelegate,UIActionSheetDelegate>
{

    UIActionSheet *actionSheet;
    UIAlertView *alert ;
}

-(void)buttonClicked:(id)sender;
@end

3.ViewDidLod中代码基本都加注释了,直接贴上代码

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    CGRect frame = CGRectMake(50, 100, 200, 70);
    
    UIButton *myButon = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    myButon.frame = frame;
//  设置视图标题
    [myButon setTitle:@"测试ActionSheet" forState:UIControlStateNormal];
//    [myButon setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//设置Button没有被选择的时候的字体颜色,选中后就变成默认的蓝色字体
    myButon.titleLabel.textColor = [UIColor blackColor];//也是字体颜色设置,和上没方法一样
    [myButon setBackgroundColor:[UIColor clearColor]];//背景色设置
//    myButon.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//button标题对齐方式,默认居中
//    给myButton添加一个事件buttonClicked
    [myButon addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
//把按钮添加到view视图上
    [self.view addSubview:myButon];
    
    
/*    //看博客时,以前没接触这个进度条控件,做测试用,搁着看的 http://www.cocoachina.com/iphonedev/toolthain/2011/1223/3778.html   
    UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
    progressView.frame = CGRectMake(20, 300, 200, 30);
    [alert addSubview:progressView];
 */
    
}

如果Button显示不出来,可以看看代码写Button遇到问题这篇博客,有些问题我这种菜鸟是解释不清楚的

4.Button点击事件,

-(void)buttonClicked:(id)sender
{
    UIAlertView *alerttest = [[UIAlertView alloc] initWithTitle:@"提示"
                                                    message:@"你点击了测试ActionSheet这个按钮" delegate:self
                                          cancelButtonTitle:@"嗯嗯 了解"
                                          otherButtonTitles:@"弹出ActionSheet吧",@"无所谓",@"有所谓", nil];
    [alerttest addButtonWithTitle:@"不发表意见"];//可以在Alrt上再添加一个Button
    alert=alerttest;
    
    [alert show];
    
}
5.这里我说一下Alert的一些委托方法,用的比较少吧,其他的也没做测试,测试也就是在委托方法里NSLog一下,再在终端下查看输出结果,通过点击按钮查看这些输出结果http://blog.youkuaiyun.com/duxinfeng2010/article/details/7702157这里面有,也就不在写出

//根据被点击按钮索引处理点击事件
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  /********************************************************************************************
   本想试试发挥下switch()的左右,可是在buttonIdex==2的时候,想加入一个Actionsheet,结果报错,说case 是保护数据,
   
   ********************************************************************************************
    switch (buttonIndex) {
        case 0:
            NSLog(@"取消按钮");
            break;
            
            case 1:
            NSLog(@"弹出ActionSheet吧");
           
            break;
            
            case 2:
            NSLog(@"有所谓了");
            break;
            
            case 3:
            NSLog(@"无所谓了");
            break;
            
            case 4:
            NSLog(@"不发表意见");
            break;
           
        default:
            break;  
    }
   ********************************************************************************************/
    if (buttonIndex == 0) {
        NSLog(@"取消按钮");
    }
    else if (buttonIndex == 1) {
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"操作选项" delegate:self
                                                  cancelButtonTitle:@"取消"
                                             destructiveButtonTitle:@"确认取消?" otherButtonTitles:@"Test1",@"Test2",@"Test4", nil];
        [sheet showInView:self.view];
        

    }
    else if(buttonIndex  == 2)
    {
        NSLog(@"有所谓了");

    }
    else if (buttonIndex == 3) {
         NSLog(@"无所谓了");
    }
else {
    NSLog(@"不发表意见");

}

}
//AlertView已经消失处理的事件
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{

}
//AlertView即将消失时,处理的事件
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{

}

//AlertView 取消时
-(void)alertViewCancel:(UIAlertView *)alertView
{

}
//AlertView已经显示的时候
-(void)didPresentAlertView:(UIAlertView *)alertView
{

}

//AlertView即将显示
-(void)willPresentAlertView:(UIAlertView *)alertView
{

}
ActionSheet和Alert写法一样,原本我想在Alert的“你点击了测试ActionSheet这个按钮”时调用我写的一个弹出ActionSheet,但是根据按钮的索引值(buttonIndex)我不知道怎么来确定这个按钮对象,也就不会怎么用addTarget: action: forControlEvents:方法来在这个委托方法外,再写一个函数了,所以也就直接在委托方法中吧ActionSheet写了,结果实现了,总感觉方法不是太好
 UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"操作选项" delegate:self
                                                  cancelButtonTitle:@"取消"
                                             destructiveButtonTitle:@"确认取消?" otherButtonTitles:@"Test1",@"Test2",@"Test4", nil];
        [sheet showInView:self.view];
6.然后把ActionSheet委托方法也写上,其实在这些委托方法里根本没多少功能,只是想让自个熟悉下都有什么委托,推荐看看这篇博客 http://blog.youkuaiyun.com/duxinfeng2010/article/details/7702169

#pragma actionSheet  delegate

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        NSLog(@"已经取消了");
    }
    if (buttonIndex == 1) {
        UIAlertView *alerttest2 = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"你在ActionSheet这个test1按钮" delegate:self
                                                  cancelButtonTitle:@"Cancel"
                                                  otherButtonTitles:@"无所谓",@"有所谓", nil];
        [alerttest2 addButtonWithTitle:@"不发表意见"];//可以在Alrt上再添加一个Button
        
        
        [alerttest2 show];
    }

}

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{

}
-(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex
{

}

-(void)actionSheetCancel:(UIActionSheet *)actionSheet
{
    

}

-(void)didPresentActionSheet:(UIActionSheet *)actionSheet
{
 
}

-(void)willPresentActionSheet:(UIActionSheet *)actionSheet
{

}

附上源代码http://download.youkuaiyun.com/detail/duxinfeng2010/4402019




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值