iPhone开发【四】常用控件之ActionSheet与AlertView
发布日期: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,添加操作:

[cpp] 
  1. <span style="font-family:Microsoft YaHei;font-size:18px;">-(IBAction)delete:(id)sender;</span>  
     将Button“删除”的Touch Up Inside与操作delete
关联起来(操作方法上一篇已讲过

3、修改Controller ViewController,让其实现协议UIActionSheetDelegate:

[cpp] 
  1. <span style="font-family:Microsoft YaHei;font-size:18px;">//修改,实现UIActionSheetDelegate协议  
  2. @interface ViewController : UIViewController<UIActionSheetDelegate></span>  
4、修改ViewController.m

     实现操作delete,如下

[cpp] 
  1. <span style="font-family:Microsoft YaHei;font-size:18px;">//执行删除操作  
  2. -(IBAction)delete:(id)sender{  
  3.     UIActionSheet *actionSheet = [[UIActionSheet alloc]  
  4.                                   initWithTitle:@"确定要删除该服务器?"   
  5.                                   delegate:self //actionSheet的代理,按钮被按下时收到通知,然后回调协议中的相关方法  
  6.                                   cancelButtonTitle:@"取消"  
  7.                                   destructiveButtonTitle:@"确定"  
  8.                                   otherButtonTitles:nil];  
  9.     //展示actionSheet  
  10.     [actionSheet showInView:self.view];  
  11. }</span>  
ViewController作为ActionSheet的代理,需要实现协议中定义的方法,有2中实现方法

方法一:实现didDismissWithButtonIndex,在ActionSheet消失后做提示处理

[cpp] 
  1. <span style="font-family:Microsoft YaHei;font-size:18px;">//方法1  
  2. -(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{  
  3.     NSLog(@"didDismissWithButtonIndex");  
  4.     UIAlertView *alert = nil;  
  5.     if(buttonIndex == [actionSheet destructiveButtonIndex]){
    //确定  
  6.         //NSLog(@"确定");  
  7.         alert = [[UIAlertView alloc]  
  8.                  initWithTitle:@"结果"   
  9.                  message:@"删除完毕"   
  10.                  delegate:self   
  11.                  cancelButtonTitle:@"确定"   
  12.                  otherButtonTitles:nil];  
  13.         [alert show];  
  14.     }else if(buttonIndex == [actionSheet cancelButtonIndex]){
    //取消  
  15.         NSLog(@"取消");  
  16.     }  
  17. }</span>  
方法而,实现clickedButtonAtIndex,在ActionSheet上的按钮被点击时做处理

[cpp] 
  1. <span style="font-family:Microsoft YaHei;font-size:18px;">//方法2  
  2. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{  
  3.     NSLog(@"clickedButtonAtIndex");  
  4.     UIAlertView *alert = nil;  
  5.     if(buttonIndex == [actionSheet destructiveButtonIndex]){
    //确定  
  6.         //NSLog(@"确定");  
  7.         //执行删除操作  
  8.         [self doDelete];  
  9.         alert = [[UIAlertView alloc]  
  10.                  initWithTitle:@"结果"   
  11.                  message:@"删除完毕"   
  12.                  delegate:self   
  13.                  cancelButtonTitle:@"确定"   
  14.                  otherButtonTitles:nil];  
  15.         [alert show];  
  16.     }else if(buttonIndex == [actionSheet cancelButtonIndex]){
    //取消  
  17.         NSLog(@"取消");  
  18.     }  
  19. }</span>  
两种方法都是通过buttonIndex判断当前点击的按钮,做不同处理

演示方法,doDelete没有实现实际操作,如下

[cpp] 
  1. <span style="font-family:Microsoft YaHei;font-size:18px;">-(void)doDelete{  
  2.     NSLog(@"执行删除操作");  
  3. }</span>  
5、编译、运行,效果如下:

转载地址:https://blog.csdn.net/h3c4lenovo/article/details/8307233 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:iPhone开发【五】常用控件之Slider(不使用xib构建UI)
下一篇:iPhone开发【三】处理基本交互

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月14日 13时40分21秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章