iOS学习之UIActionSheet的使用

简介: UIActionSheet是在iOS弹出的选择按钮项,可以添加多项,并为每项添加点击事件。 为了快速完成这例子,我们打开Xcode 4.3.2, 先建立一个single view application。

 

UIActionSheet是在iOS弹出的选择按钮项,可以添加多项,并为每项添加点击事件。

为了快速完成这例子,我们打开Xcode 4.3.2, 先建立一个single view application。然后再xib文件添加一个button,用来弹出sheet view。

1、首先在.h文件中实现协议

加代码的地方在@interface那行的最后添加<UIActionSheetDelegate>,协议相当于java里的接口,实现协议里的方法。

[cpp]   view plain copy
  1. @interface sheetviewViewController : UIViewController<UIActionSheetDelegate>  
  2.   
  3. @end  

 

2、添加button,命名button为showSheetView.

3、为button建立Action映射,映射到.h文件上,事件类型为Action ,命名为showSheet。

4、在.m文件上添加点击事件代码

图的效果是这样的:

 

[cpp]   view plain copy
  1. - (IBAction)showSheet:(id)sender {  
  2.     UIActionSheet *actionSheet = [[UIActionSheet alloc]  
  3.                                   initWithTitle:@"title,nil时不显示"  
  4.                                   delegate:self  
  5.                                   cancelButtonTitle:@"取消"  
  6.                                   destructiveButtonTitle:@"确定"  
  7.                                   otherButtonTitles:@"第一项", @"第二项",nil];  
  8.     actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;  
  9.     [actionSheet showInView:self.view];  
  10. }  

actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;//设置样式

参数解释:   

cancelButtonTitle  destructiveButtonTitle是系统自动的两项。

otherButtonTitles是自己定义的项,注意,最后一个参数要是nil。

 

[actionSheet showInView:self.view];这行语句的意思是在当前view显示Action sheet。当然还可以用其他方法显示Action sheet。

对应上面的图和代码,一目了然了把

5、接下来我们怎么相应Action Sheet的选项的事件呢?

实现协议里的方法。为了能看出点击Action sheet每一项的效果,我们加入UIAlertView来做信息显示。下面是封装的一个方法,传入对应的信息,在UIAlertView显示对应的信息。

[cpp]   view plain copy
  1. -(void)showAlert:(NSString *)msg {  
  2.     UIAlertView *alert = [[UIAlertView alloc]  
  3.                           initWithTitle:@"Action Sheet选择项"  
  4.                           message:msg  
  5.                           delegate:self  
  6.                           cancelButtonTitle:@"确定"  
  7.                           otherButtonTitles: nil];  
  8.     [alert show];  
  9. }  

那相应被Action Sheet选项执行的代码如下:

[cpp]   view plain copy
  1. (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex  
  2. {  
  3.     if (buttonIndex == 0) {  
  4.         [self showAlert:@"确定"];  
  5.     }else if (buttonIndex == 1) {  
  6.         [self showAlert:@"第一项"];  
  7.     }else if(buttonIndex == 2) {  
  8.         [self showAlert:@"第二项"];  
  9.     }else if(buttonIndex == 3) {  
  10.         [self showAlert:@"取消"];  
  11.     }   
  12.   
  13. }  
  14. - (void)actionSheetCancel:(UIActionSheet *)actionSheet{    
  15.   
  16. }    
  17. -(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{    
  18.   
  19. }    
  20. -(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{    
  21.   
  22. }  

可以看到 buttonIndex 是对应的项的索引。

看到那个红色的按钮没?那是ActionSheet支持的一种所谓的销毁按钮,对某户的某个动作起到警示作用,

比如永久性删除一条消息或图像时。如果你指定了一个销毁按钮他就会以红色高亮显示:

actionSheet.destructiveButtonIndex=1;  

与导航栏类似,操作表单也支持三种风格 

UIActionSheetStyleDefault              //默认风格:灰色背景上显示白色文字   

UIActionSheetStyleBlackTranslucent     //透明黑色背景,白色文字   

UIActionSheetStyleBlackOpaque          //纯黑背景,白色文字  

用法:

 actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;//设置样式

我选sheet 里的第一项,显示如下:

 

6、注意事项

在开发过程中,发现有时候UIActionSheet的最后一项点击失效,点最后一项的上半区域时有效,这是在特定情况下才会发生,这个场景就是试用了UITabBar的时候才有。解决办法:

在showView时这样使用,[actionSheet showInView:[UIApplication sharedApplication].keyWindow];或者[sheet showInView:[AppDelegate sharedDelegate].tabBarController.view];这样就不会发生遮挡现象了。

 

代码获取:http://download.csdn.net/detail/totogo2010/4343267

https://github.com/schelling/YcDemo

著作权声明:本文由http://blog.csdn.net/totogo2010/原创

img_e00999465d1c2c1b02df587a3ec9c13d.jpg
微信公众号: 猿人谷
如果您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】
如果您希望与我交流互动,欢迎关注微信公众号
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

目录
相关文章
|
5月前
|
安全 前端开发 Android开发
鸿蒙开发|鸿蒙系统的介绍(为什么要学习鸿蒙开发|鸿蒙系统的官方定义|鸿蒙和安卓、ios的对比)
鸿蒙开发学习是一项探索性的工作,旨在开发一个全场景分布式操作系统,覆盖所有设备,让消费者能够更方便、更直观地使用各种设备。
290 6
鸿蒙开发|鸿蒙系统的介绍(为什么要学习鸿蒙开发|鸿蒙系统的官方定义|鸿蒙和安卓、ios的对比)
|
7月前
|
iOS开发
iOS UIKit Dynamics Demo 学习地址列表
iOS UIKit Dynamics Demo 学习地址列表
23 0
|
iOS开发
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
126 0
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
|
物联网 Android开发 iOS开发
iOS开发 - 蓝牙学习的总结
iOS开发 - 蓝牙学习的总结
128 0
|
存储 Unix 编译器
|
存储 算法 iOS开发
|
存储 缓存 算法
iOS底层学习——对象初始化探索
iOS底层学习——对象初始化探索
|
iOS开发 索引
IOS中UIActionSheet使用详解
IOS中UIActionSheet使用详解
255 0
|
API iOS开发 开发者
学习iOS开发如何进阶?
前言 如果你有志于将iOS开发作为职业,或者已经是一位iOS开发者,那么你应该听说过唐巧的名字。唐巧,2012年从网易有道离开参与创业, 目前是猿题库iOS高级研发工程师。
2166 0
|
人工智能 TensorFlow 算法框架/工具
客户端码农学习ML —— 工具框架Tensorflow及Android、iOS上初步实验
与其上来就学习相对枯燥易让人放弃的数学,不如先做几个例子并在Android、iOS上熟悉下整个操作流程,通过实战激发下兴趣。 开发环境准备 首先安装Python,推荐Python3,装好后别忘了设置下载源镜像,不然安装各种包的时候下载速度很感人。
24710 0