代码实现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.csdn.net/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.csdn.net/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.csdn.net/detail/duxinfeng2010/4402019





  本文转自新风作浪 51CTO博客,原文链接:http://blog.51cto.com/duxinfeng/1208756,如需转载请自行联系原作者


相关文章
uniapp switch按钮的使用开关按钮效果demo(整理)
uniapp switch按钮的使用开关按钮效果demo(整理)
|
9月前
Echarts实战案例代码(40):tooltip使用formatter函数判断是否显示提示内容
Echarts实战案例代码(40):tooltip使用formatter函数判断是否显示提示内容
62 0
|
前端开发 JavaScript 数据格式
el-menu导航菜单的二次封装(递归组件)实现动态多级菜单
el-menu导航菜单的二次封装(递归组件)实现动态多级菜单
551 0
UIAlertController简单使用
UIAlertController简单使用
84 0
UIAlertController简单使用
|
JSON 数据格式
基于el-menu,用递归实现动态n级菜单
element-plus 的 el-menu 菜单组件,功能非常强大,支持n级菜单、图标等等功能。如果菜单是静态的,直接手撸的话,那么按照官网实例即可,但是如果需要基于json动态绑定,而且还是n级菜单,那么要怎么办呢?
895 0
|
Dart API 开发者
【Flutter】Hero 动画 ( Hero 动画使用流程 | 创建 Hero 动画核心组件 | 创建源页面 | 创建目的页面 | 页面跳转 )
【Flutter】Hero 动画 ( Hero 动画使用流程 | 创建 Hero 动画核心组件 | 创建源页面 | 创建目的页面 | 页面跳转 )
183 0
【Flutter】Hero 动画 ( Hero 动画使用流程 | 创建 Hero 动画核心组件 | 创建源页面 | 创建目的页面 | 页面跳转 )