1、创建一个Single View Application工程,命名为:ActionSheetDemo,如下图
2、在ViewController.xib上放置一个Button,Title为“删除”
修改ViewController.h,添加操作:
将Button“删除”的Touch Up Inside与操作delete关联起来(操作方法上一篇已讲过)
3、修改Controller ViewController,让其实现协议UIActionSheetDelegate:
4、修改ViewController.m
实现操作delete,如下
ViewController作为ActionSheet的代理,需要实现协议中定义的方法,有2中实现方法
方法一:实现didDismissWithButtonIndex,在ActionSheet消失后做提示处理
方法二,实现clickedButtonAtIndex,在ActionSheet上的按钮被点击时做处理
两种方法都是通过buttonIndex判断当前点击的按钮,做不同处理
演示方法,doDelete没有实现实际操作,如下
5、编译、运行,效果如下:
2、在ViewController.xib上放置一个Button,Title为“删除”
修改ViewController.h,添加操作:
-(IBAction)delete:(id)sender;
将Button“删除”的Touch Up Inside与操作delete关联起来(操作方法上一篇已讲过)
3、修改Controller ViewController,让其实现协议UIActionSheetDelegate:
//修改,实现UIActionSheetDelegate协议 @interface ViewController : UIViewController
4、修改ViewController.m
实现操作delete,如下
//执行删除操作 -(IBAction)delete:(id)sender{ UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"确定要删除该服务器?" delegate:self //actionSheet的代理,按钮被按下时收到通知,然后回调协议中的相关方法 cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil]; //展示actionSheet [actionSheet showInView:self.view]; }
ViewController作为ActionSheet的代理,需要实现协议中定义的方法,有2中实现方法
方法一:实现didDismissWithButtonIndex,在ActionSheet消失后做提示处理
//方法1 -(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{ NSLog(@"didDismissWithButtonIndex"); UIAlertView *alert = nil; if(buttonIndex == [actionSheet destructiveButtonIndex]){//确定 //NSLog(@"确定"); alert = [[UIAlertView alloc] initWithTitle:@"结果" message:@"删除完毕" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alert show]; }else if(buttonIndex == [actionSheet cancelButtonIndex]){//取消 NSLog(@"取消"); } }
方法二,实现clickedButtonAtIndex,在ActionSheet上的按钮被点击时做处理
//方法2 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"clickedButtonAtIndex"); UIAlertView *alert = nil; if(buttonIndex == [actionSheet destructiveButtonIndex]){//确定 //NSLog(@"确定"); //执行删除操作 [self doDelete]; alert = [[UIAlertView alloc] initWithTitle:@"结果" message:@"删除完毕" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alert show]; }else if(buttonIndex == [actionSheet cancelButtonIndex]){//取消 NSLog(@"取消"); } }
两种方法都是通过buttonIndex判断当前点击的按钮,做不同处理
演示方法,doDelete没有实现实际操作,如下
-(void)doDelete{ NSLog(@"执行删除操作");
5、编译、运行,效果如下:
收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2312
- 用户1336
- 访客11623913
每日一句
Compliment yourself daily.
每天夸自己一句。
每天夸自己一句。
How to Ungroup Icons on Windows 11 Taskbar With a Registry Hack (and 2 More Ways)
反编译修改class文件变量
如何在大学成为一名优秀的程序员?
VMware NAT端口映射外网访问虚拟机linux
ubuntu下提取DSDT SSDT
使用HTML和CSS设计磨砂玻璃效果
解决android studio 4.4使用javah失败
vscode使用eslint自动代码格式化
c++浮点运算能力附安卓版
【源码】两种仪表盘
P2P中NAT之间的打洞可能性
jQuery的load方法Cannot read property 'indexOf' of undefined
【开源Roguelike游戏】素地牢源码
新会员