iOS中UIActionSheet的使用

谁能告诉我这个控件的中文名应该叫啥...找资料时都是直接找的UIActionSheet,想叫中文名字突然觉得想不出来了...

学习这个的初始目的是学习从拍照或者相册选择图片使用,感觉这个情况也是这个控件最常见的用处了,当用户要改变头像时,点击一下头像出现这个东西供用户选择图片来源,如下图:


相信都见过这个控件吧,不过它的中文名到底叫啥来着...

要做这个特备简单,这里直接贴代码吧,我的注释里应该讲的很清楚了,我是点击更换头像的Button来弹出这个选择界面的,所以关于UIActionSheet的创建和显示都在Button的响应方法中。

另外还有要注意这个控件是有委托的,所以要在.h文件中说明。

基本常用的方法和委托都写到了,具体见代码吧:

//.h文件中
@interface ViewController : UIViewController <UIActionSheetDelegate>

- (IBAction)changeAvata:(id)sender;

@end

//.m文件中

- (IBAction)changeAvata:(id)sender {
    //创建一个UIActionSheet,其中destructiveButton会红色显示,可以用在一些重要的选项
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"更换头像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从相册选择", nil];
    
    //actionSheet风格,感觉也没什么差别- -
    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;//默认风格,灰色背景,白色文字
//    actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
//    actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
//    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;//纯黑背景,白色文字
    
    //如果想再添加button
//    [actionSheet addButtonWithTitle:@"其他方式"];
    
    //更改ActionSheet标题
//    actionSheet.title = @"选择照片";
    
    //获取按钮总数
    NSString *num = [NSString stringWithFormat:@"%ld", actionSheet.numberOfButtons];
    NSLog(@"%@", num);
    
    //获取某个索引按钮的标题
    NSString *btnTitle = [actionSheet buttonTitleAtIndex:1];
    NSLog(@"%@", btnTitle);
    
    [actionSheet showInView:self.view];
    
    
}

#pragma mark - UIActionSheetDelegate
//根据被点击的按钮做出反应,0对应destructiveButton,之后的button依次排序
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        NSLog(@"拍照");
    }
    else if (buttonIndex == 2) {
        NSLog(@"相册");
    }
}

//取消ActionSheet时调用
- (void)actionSheetCancel:(UIActionSheet *)actionSheet {
    
}

//将要显示ActionSheet时调用
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
    
}

//已经显示ActionSheet是调用
-(void)didPresentActionSheet:(UIActionSheet *)actionSheet {
    
}

//ActionSheet已经消失时调用
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    
}

//ActionSheet即将消失时调用
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
    
}

基本的使用方法就是上面这样啦,有其他的也希望能多多交流~

这里有我的工程源码https://github.com/Cloudox/UIActionSheetTest

一开始也说了本来是学习换头像的,所以工程名不太对,见谅哈

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值