zoukankan      html  css  js  c++  java
  • 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,
    };
    

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

  • 相关阅读:
    hdu 3666 差分约束系统
    hdu 1198农田灌溉
    常微分方程(阿諾爾德) Page 45 相空間,相流,運動,相曲線 註記
    高等微積分(高木貞治) 1.4節 例2
    常微分方程(阿諾爾德) Page 45 相空間,相流,運動,相曲線 註記
    解析函數論 Page 29 命題(2) 函數模的有界性
    高等微積分(高木貞治) 1.4節 例2
    解析函數論 Page 29 命題(1) 有界閉集上的一致連續性
    解析函數論 Page 29 命題(3) 模的下界的可達性
    解析函數論 Page 29 命題(2) 函數模的有界性
  • 原文地址:https://www.cnblogs.com/xiaofeixiang/p/4521610.html
Copyright © 2011-2022 走看看