欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

IOS基础:ActionSheet(上拉菜单)的实现

发布时间:2025/3/15 编程问答 27 豆豆
生活随笔 收集整理的这篇文章主要介绍了 IOS基础:ActionSheet(上拉菜单)的实现 小编觉得挺不错的,现在分享给大家,帮大家做个参考.


一看图就明白了,毋需多说。

 

[java] view plaincopyprint?
  • UIActionSheet* mySheet = [[UIActionSheet alloc]  
  •                            initWithTitle:@"ActionChoose"   
  •                            delegate:self   
  •                            cancelButtonTitle:@"Cancel"  
  •                            destructiveButtonTitle:@"Destroy"  
  •                            otherButtonTitles:@"OK", nil];  
  •     [mySheet showInView:self.view];  
  • 与UIAlertView类似,我们也是在委托方法里处理按下按钮后的动作。记得在所委托的类加上UIActionSheetDelegate。

     

    [java] view plaincopyprint?
  • - (void)actionSheetCancel:(UIActionSheet *)actionSheet{  
  •     //   
  • }  
  • - (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{  
  •     //   
  • }  
  • -(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{  
  •     //   
  • }  
  • -(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{  
  •     //   
  • }  
  • 看到那个红色的按钮没?那是ActionSheet支持的一种所谓的销毁按钮,对某户的某个动作起到警示作用,

    比如永久性删除一条消息或者日志。如果你指定了一个销毁按钮他就会以红色高亮显示:

     

    [java] view plaincopyprint?
  • mySheet.destructiveButtonIndex=1;  
  • 与导航栏类似,操作表单也支持三种风格 :

     

     

    [java] view plaincopyprint?
  • UIActionSheetStyleDefault              //默认风格:灰色背景上显示白色文字   
  • UIActionSheetStyleBlackTranslucent     //透明黑色背景,白色文字   
  • UIActionSheetStyleBlackOpaque          //纯黑背景,白色文字  
  • 用法用例:

     

    mySheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;

    显示ActionSheet有三种方法:

    1.在一个视图内部显示,可以用showInView

    [mySheet showInView:self];

    2.如果要将ActonSheet 与工具栏或者标签栏对齐,可以使用showFromToolBar或showFromTabBar

    [mySheet showFromToolBar:toolbar];

    [mySheet showFromTabBar:tabbar];
    解除操作表单

    用户按下按钮之后,Actionsheet就会消失——除非应用程序有特殊原因,需要用户按下做个按钮。用dismiss方法可令表单消失:

     

    [java] view plaincopyprint?
  • [mySheet dismissWithClickButtonIndex:1 animated:YES];  

  •  

    必须使用Protocol,在类定义的地方定义使用UIActionSheetDelegate协议,

    @interface XXXController : UIViewController <UIActionSheetDelegate> {...

     

    在程序里面调用

    UIActionSheet *actionSheet = [[UIActionSheet alloc]

      initWithTitle:@"Are you sure?"         //标题

      delegate:self                  //此处指定处理按钮按下之后的事件的类,该类必须实现UIActionSheetDelegate协议

      cancelButtonTitle:@"Cancel" 

      destructiveButtonTitle:@"OK"

      otherButtonTitles:@"button1", @"button2", nil];  //可指定很多个button,最后一个参数必须为nil,此为OBJC特殊特性

    [actionSheet showInView:self.view];   //在哪个view里面弹出上拉菜单

    [actionSheet release];    //一定要release

    记得最后一定要release!

     

    处理按钮事件的方法为实现UIActionSheetDelegate协议的actionSheet方法:

    - (void)actionSheet:(UIActionSheet *)actionSheet

    didDismissWithButtonIndex:(NSInteger)buttonIndex

    {

        if( buttonIndex != [actionSheet cancelButtonIndex]){

            //...

        }

    }

    转载于:https://www.cnblogs.com/martin1009/archive/2012/06/25/2561362.html

    总结

    以上是生活随笔为你收集整理的IOS基础:ActionSheet(上拉菜单)的实现的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。