• iOS开发-UIActionSheet简单介绍


    UIActionSheet和UIAlertView都是ios系统自带的模态视图,模态视图的一个重要的特性就是在显示模态视图的时候可以阻断其他视图的事件响应。一般情况下我们对UIAlertView使用的比较多,UIActionSheet相对来说情况少一点,偶尔作为一个上拉菜单来展示还是非常有用的。通常如果显示一个模态的视图,可以自定义一个UIViewController,不过里面的内容和动画实现起来工作量还是非常多的。

    UIActionSheet介绍

    介绍UIActionSheet之前需要简单的看下效果实现:

    这是最基本的UIActionSheet,标题和按钮,不过有的时候为了美观可以不用标题,效果看起来是非常赞的,先上代码之后讲解:

      UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                          initWithTitle:@"博客园"
                                          delegate:self
                                          cancelButtonTitle:@"取消"
                                          destructiveButtonTitle:@"确定"
                                          otherButtonTitles:@"keso", @"FlyElephant",nil];
            actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
            [actionSheet showInView:self.view];
    

    大家可以明显感觉到确定是白底红字,其他的按钮是白底蓝字,因为确定在这里是destructiveButton,相当于销毁就是给用户一个明显的提示,这里的显示是直接在View中显示,当然也有其他的地方显示,调用方法如下:

    - (void)showFromToolbar:(UIToolbar *)view;
    - (void)showFromTabBar:(UITabBar *)view;
    - (void)showFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated NS_AVAILABLE_IOS(3_2);
    - (void)showFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated NS_AVAILABLE_IOS(3_2);
    - (void)showInView:(UIView *)view;
    

    跟大多数控件一下,UIActionSheet也是有代理的,实现UIActionSheetDelegate可以在按钮之后执行自己需要执行的事件:

    -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
        NSString  *result=@"";
        switch (buttonIndex) {
            case 0:
                result=@"确定";
                break;
            case 1:
                result=@"keso";
                break;
            case 2:
                result=@"FlyElephant";
                break;
            case 3:
                result=@"取消";
                break;
        }
    
        UIAlertView  *alertView=[[UIAlertView alloc]initWithTitle:@"测试" message:result delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertView show];
    }
    -(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonInde{
         NSLog(@"didDismissWithButtonIndex-FlyElphant");
    }
    -(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{
         NSLog(@"willDismissWithButtonIndex-FlyElphant");
    }
    -(void)actionSheetCancel:(UIActionSheet *)actionSheet{
        NSLog(@"Home键执行此方法");
    }
    

     四个方法都是比较常见的使用,一般情况下只需要调用第一个方法即可,actionSheetCancel网上说的比较少,这个方法一般是点击Home键的时候执行,不是点击取消的按钮的执行,详情可参考文档解释:

    // Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
    // If not defined in the delegate, we simulate a click in the cancel button
    

     UIActionSheet的样式默认样式,黑色半透明和黑色不透明三种样式:

    typedef NS_ENUM(NSInteger, UIActionSheetStyle) {
        UIActionSheetStyleAutomatic        = -1,       // take appearance from toolbar style otherwise uses 'default'
        UIActionSheetStyleDefault          = UIBarStyleDefault,
        UIActionSheetStyleBlackTranslucent = UIBarStyleBlackTranslucent,
        UIActionSheetStyleBlackOpaque      = UIBarStyleBlackOpaque,
    };
    

      如果你觉得系统的不爽,可以自定义,完全取决于个人需求~

  • 相关阅读:
    转载: jQuery事件委托( bind() live() delegate()) [委托 和 绑定的故事]
    转载:CPU的位数和操作系统的位数
    javascript 过滤空格
    转载: js jquery 获取当前页面的url,获取frameset中指定的页面的url(有修改)
    转载:struts标签<s:date>的使用
    转载:s:if的用法
    解决cordova-plugin-actionsheet导致Android丑陋的问题
    ionic框架对Android返回键的处理
    解决魅族手机无法连接Mac调试
    谷歌开发者大会传达的8条关键信息
  • 原文地址:https://www.cnblogs.com/xiaofeixiang/p/4521610.html
Copyright © 2020-2023  润新知