UIActionSheet的使用
2019独角兽企业重金招聘Python工程师标准>>>
UIActionSheet是在IOS弹出的选择按钮项,可以添加多项,并为每项添加点击事件。
为了快速完成这例子,我们打开Xcode 4.3.2, 先建立一个single view application。然后再xib文件添加一个button,用来弹出sheet view。
1、首先在.h文件中实现协议,加代码的地方在@interface那行的最后添加<UIActionSheetDelegate>,协议相当于java里的接口,实现协议里的方法。
- @interface sheetviewViewController : UIViewController<UIActionSheetDelegate>
- @end
2、添加button,命名button为showSheetView.
3、为button建立Action映射,映射到.h文件上,事件类型为Action ,命名为showSheet。
4、在.m文件上添加点击事件代码
图的效果是这样的:
- - (IBAction)showSheet:(id)sender {
- UIActionSheet *actionSheet = [[UIActionSheet alloc]
- initWithTitle:@"title,nil时不显示"
- delegate:self
- cancelButtonTitle:@"取消"
- destructiveButtonTitle:@"确定"
- otherButtonTitles:@"第一项", @"第二项",nil];
- actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
- [actionSheet showInView:self.view];
- }
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;//设置样式
参数解释:
cancelButtonTitle destructiveButtonTitle是系统自动的两项。
otherButtonTitles是自己定义的项,注意,最后一个参数要是nil。
[actionSheet showInView:self.view];这行语句的意思是在当前view显示Action sheet。当然还可以用其他方法显示Action sheet。
对应上面的图和代码,一目了然了把
5、接下来我们怎么相应Action Sheet的选项的事件呢?实现协议里的方法。为了能看出点击Action sheet每一项的效果,我们加入UIAlertView来做信息显示。下面是封装的一个方法,传入对应的信息,在UIAlertView显示对应的信息。
- -(void)showAlert:(NSString *)msg {
- UIAlertView *alert = [[UIAlertView alloc]
- initWithTitle:@"Action Sheet选择项"
- message:msg
- delegate:self
- cancelButtonTitle:@"确定"
- otherButtonTitles: nil];
- [alert show];
- }
那相应被Action Sheet选项执行的代码如下:
- <span style="font-family: 'Microsoft YaHei', 微软雅黑, tahoma, arial, simsun, 宋体; ">-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
- {
- if (buttonIndex == 0) {
- [self showAlert:@"确定"];
- }else if (buttonIndex == 1) {
- [self showAlert:@"第一项"];
- }else if(buttonIndex == 2) {
- [self showAlert:@"第二项"];
- }else if(buttonIndex == 3) {
- [self showAlert:@"取消"];
- }
- }
- - (void)actionSheetCancel:(UIActionSheet *)actionSheet{
- }
- -(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
- }
- -(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{
- }
- </span>
可以看到 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
转载于:https://my.oschina.net/jackyyang/blog/67108
UIActionSheet的使用相关推荐
- UIActionSheet在iOS8中被弃用造成的错误
UIActionSheet在iOS7.0中效果图如下: UIActionSheet在iOS8中效果图如下: ...
- IOS问题汇总:2012-12-18 UIAlertView+UIActionSheet
UIAlertView/UIActionSheet UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"添加场景模式 ...
- iOS中UIActionSheet使用详解
一.初始化方法 - (instancetype)initWithTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)de ...
- IOS学习笔记(九)之UIAlertView(警告视图)和UIActionSheet(操作表视图)基本概念和使用方法...
IOS学习笔记(九)之UIAlertView(警告视图)和UIActionSheet(操作表视图)基本概念和使用方法 Author:hmjiangqq Email:jiangqqlmj@163.com ...
- iOS UI基础-6.0 UIActionSheet的使用
UIActionSheet是在iOS弹出的选择按钮项,可以添加多项,并为每项添加点击事件. 使用 1.需要实现UIActionSheetDelegate 协议 @interface NJWisdom ...
- UIActionSheet和UIProgressView的组合
2019独角兽企业重金招聘Python工程师标准>>> - (void) incrementBar: (id) timer {amountDone += 1.0f;[progress ...
- IOS中UIActionSheet使用方法详解
一.初始化方法 - (instancetype)initWithTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)de ...
- IOS开发之自定义UIActionSheet
IOS开发中,经常会用到UIActionSheet,但是,默认的只能添加按钮.如果能自定义的话,岂不是更好?上网搜了一下,都是只有那一种代码,通过设置几个按钮来增加UIActionSheet的高度,不 ...
- 【转】iOS开发6:UIActionSheet与UIAlertView
原文: http://my.oschina.net/plumsoft/blog/42763 iOS程序中的Action Sheet就像Windows中的 "确定-取消"对话框一样, ...
- UIActionSheet
我感觉UIActionSheet和UIAlertView的用法差不多,都很简单,下面给出一个简单的Demo,具体想用哪个,根据公司要求和个人爱好. #import "ViewControll ...
最新文章
- mysql行列转换例子_mysql行列转换示例
- 企业 SOA 设计(1)–ESB 设计
- GDB中应该知道的几个调试方法
- 1.15 Java 9新增的不可变集合
- python中import与input_python : import详解。
- brand.php dnfire.cn_能美火灾报警_能美西科姆消防报警主机如何屏蔽故障点_滁州气象...
- Cordova内部http请求的proxy实现原理
- HttpWebRequest post上传文件
- java内存管理总结
- js中值得推荐的Memoization
- node.js 之爬虫
- 2016年第四季度思科称霸印度网络市场
- c语言打字游戏程序设计报告,2016打字游戏程序设计报告.doc
- cab文件打包的ActiveX控件(转)
- 黑马程序员Python教程的代码--植物大战僵尸游戏代码
- ffmpeg合并多mp4视频
- 【Adobe】Photoshop :Windows 系统 Photoshop 软件更换许可指引
- 教你利用VMM虚拟机安装LEDE旁路由实现软路由超强功能的方法教程
- asp.net实现无刷新ajax技术登录界面
- c语言else if设计一个成绩表,c语言 学生成绩管理系统设计学生成绩信息包括:学号,姓名,三门课程成绩(数学、英语和计算机)等。主要功能:(1) 计算各...
热门文章
- C++安全方向openssl(一):1.1 openssl3.0介绍以及在windows2019下使用vs2019
- 一个黑色全屏的计时器_佳作分享最佳倒数计时器设计分析【附原型实例】
- 小程序登录本地服务器,微信小程序实现用户登录模块服务器搭建
- windbg linux内核调试,使用Windbg调试window内核
- PythonEggs
- R语言高级算法之人工神经网络(Artificial Neural Network)
- C/C++语言开发环境的搭建
- 2020年已裸辞5个月(软文)
- CentOS网络配置解决方案
- Maven学习总结(37)——利用GitHub或阿里云OSS对象存储、又拍云、七牛云存储等搭建个人Maven仓库