iPhone开发【四】常用控件之ActionSheet与AlertView
来源:互联网 发布:华为畅享5手机壳淘宝 编辑:程序博客网 时间:2024/05/22 06:11
转载请注明出处,原文网址:http://blog.csdn.net/m_changgong/article/details/8103321 作者:张燕广
实现的功能:练习ActionSheet与AlertView的使用方法
关键词:ActionSheet AlertView
1、创建一个Single View Application工程,命名为:ActionSheetDemo,如下图
2、在ViewController.xib上放置一个Button,Title为“删除”
修改ViewController.h,添加操作:
-(IBAction)delete:(id)sender;
将Button“删除”的Touch Up Inside与操作delete关联起来(操作方法上一篇已讲过)3、修改Controller ViewController,让其实现协议UIActionSheetDelegate:
//修改,实现UIActionSheetDelegate协议@interface ViewController : UIViewController<UIActionSheetDelegate>
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、编译、运行,效果如下:
点击下载本文源代码
- iPhone开发【四】常用控件之ActionSheet与AlertView
- iPhone开发【四】常用控件之ActionSheet与AlertView
- iphone开发之使用ActionSheet
- Iphone开发(六)IOS中的通知--操作表ActionSheet和警报AlertView
- Iphone开发(六)IOS中的通知--操作表ActionSheet和警报AlertView
- Iphone开发(六)IOS中的通知--操作表ActionSheet和警报AlertView
- Iphone开发(六)IOS中的通知--操作表ActionSheet和警报AlertView
- AlertView&ActionSheet
- iphone之ActionSheet介绍
- iPhone开发【十八】自定义视图之ActionSheet中使用PickerView
- iPhone开发【十八】自定义视图之ActionSheet中使用PickerView
- iOS AlertView、ActionSheet
- iPhone 开发常用控件
- IOS8 alertView 和 actionSheet 使用方法
- Actionsheet和AlertView的使用
- ActionSheet和AlertView的封装
- iphone开发之常用控件的使用详解
- iPhone开发【六】常用控件之Picker View
- I-jetty的web应用
- 十月23日
- 借助磁盘修理工具解决Boot Camp助理的分区问题
- SQL Server 2005 sa用户登录失败?
- 内存操作函数(C)
- iPhone开发【四】常用控件之ActionSheet与AlertView
- Intent的FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_REORDER_TO_FRONT
- pscp详解
- Socket通讯(objetive-c)客户端与(JAVA)服务端的特殊点
- 小小耳机大学问
- 正则表达式-将秒数转化为时间格式
- 任小笨空间伤感日志:享受瞬间的华丽、绽放凄美的诀别
- Select()系统调用及文件描述符集fd_set的应用
- ubuntu 邮件系统附件直接编辑后不知道保存到那里了,怎么办?