iOS UIActionSheet的用法

UIActionSheet是和UIAlertView风格差不多的两个UI控件,不过他们的作用不同,UIAlertView用来作为警告框或者账号密码输入框,而UIActionSheet是用来选择的控件,比如微信朋友圈中的图片选择功能就是这个控件。

还有就是UIAlertView的按钮在View中间,而UIActionSheet的按钮在底部,实现的功能基本都能互换,看个人需求使用哪个哈。

看例子:首先创建对象,然后showInView 显示在界面上

    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"这是UIActionSheet"
                                  delegate:self
                                  cancelButtonTitle:@"取消"
                                  destructiveButtonTitle:@"确定"
                                  otherButtonTitles:@"按钮1", @"按钮2",nil];
    actionSheet.actionSheetStyle = UIBarStyleDefault;
    [actionSheet showInView:self.view];

实现delegate协议,把delegate设为self

@interface ViewController : UIViewController<UIActionSheetDelegate>

@end

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 1:
            NSLog(@"按钮1");
            break;
        case 2:
            NSLog(@"按钮2");
            break;
        case 3:
            NSLog(@"取消");
            break;
        case 0:
            NSLog(@"确定");
            break;
        default:
            break;
    }
    
}

点击相应的按钮触发相应的事件

效果图:


其他一些回调方法:

- (void)actionSheetCancel:(UIActionSheet *)actionSheet{
    NSLog(@"取消");
}
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
    
}
-(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{
    
}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值