iPhone开发【四】常用控件之ActionSheet与AlertView
将Button“删除”的Touch Up Inside与操作delete 关联起来(操作方法上一篇已讲过) 4、修改ViewController.m ViewController作为ActionSheet的代理,需要实现协议中定义的方法,有2中实现方法 方法而,实现clickedButtonAtIndex,在ActionSheet上的按钮被点击时做处理 两种方法都是通过buttonIndex判断当前点击的按钮,做不同处理 5、编译、运行,效果如下:
发布日期:2021-09-28 18:46:09
浏览次数:9
分类:技术文章
本文共 2432 字,大约阅读时间需要 8 分钟。
转载请注明出处,原文网址: 作者:张燕广
实现的功能:练习ActionSheet与AlertView的使用方法
关键词:ActionSheet AlertView
1、创建一个Single View Application工程,命名为:ActionSheetDemo,如下图
2、在ViewController.xib上放置一个Button,Title为“删除”
修改ViewController.h,添加操作:
- <span style="font-family:Microsoft YaHei;font-size:18px;">-(IBAction)delete:(id)sender;</span>
3、修改Controller ViewController,让其实现协议UIActionSheetDelegate:
- <span style="font-family:Microsoft YaHei;font-size:18px;">//修改,实现UIActionSheetDelegate协议
- @interface ViewController : UIViewController<UIActionSheetDelegate></span>
实现操作delete,如下
- <span style="font-family:Microsoft YaHei;font-size:18px;">//执行删除操作
- -(IBAction)delete:(id)sender{
- UIActionSheet *actionSheet = [[UIActionSheet alloc]
- initWithTitle:@"确定要删除该服务器?"
- delegate:self //actionSheet的代理,按钮被按下时收到通知,然后回调协议中的相关方法
- cancelButtonTitle:@"取消"
- destructiveButtonTitle:@"确定"
- otherButtonTitles:nil];
- //展示actionSheet
- [actionSheet showInView:self.view];
- }</span>
方法一:实现didDismissWithButtonIndex,在ActionSheet消失后做提示处理
- <span style="font-family:Microsoft YaHei;font-size:18px;">//方法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(@"取消");
- }
- }</span>
- <span style="font-family:Microsoft YaHei;font-size:18px;">//方法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(@"取消");
- }
- }</span>
演示方法,doDelete没有实现实际操作,如下
- <span style="font-family:Microsoft YaHei;font-size:18px;">-(void)doDelete{
- NSLog(@"执行删除操作");
- }</span>
转载地址:https://blog.csdn.net/h3c4lenovo/article/details/8307233 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2024年04月14日 13时40分21秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
spring boot 与 Ant Design of Vue 实现删除组织(二十六)
2021-06-30
spring boot 与 Ant Design of Vue 实现删除用户(三十)
2021-06-30
Druid连接池实现自定义场景的多数据库的连接
2021-06-30
PL/SQL数据库管理工具的使用
2021-06-30
带你玩转属于自己的spring-boot-starter系列(一)
2021-06-30
带你玩转属于自己自己的spring-boot-starter系列(二)
2019-04-27
带你玩转属于自己的spring-boot-starter系列(三)
2019-04-27
基于SnowFlake算法如何让分库分表中不同的ID落在同一个库的算法的实现
2019-04-27
Linux文件管理参考
2019-04-27
FTP文件管理项目(本地云)项目日报(二)
2019-04-27
FTP文件管理项目(本地云)项目日报(三)
2019-04-27
FTP文件管理项目(本地云)项目日报(七)
2019-04-27
FTP文件管理项目(本地云)项目日报(九)
2019-04-27
以练代学设计模式 -- FTP文件管理项目
2019-04-27
FTP文件管理项目(本地云)项目日报(十)
2019-04-27
学以致用设计模式 之 “组合模式”
2019-04-27