相信都见过这个控件吧,不过它的中文名到底叫啥来着...
要做这个特备简单,这里直接贴代码吧,我的注释里应该讲的很清楚了,我是点击更换头像的Button来弹出这个选择界面的,所以关于UIActionSheet的创建和显示都在Button的响应方法中。
另外还有要注意这个控件是有委托的,所以要在.h文件中说明。
基本常用的方法和委托都写到了,具体见代码吧:
-
- @interface ViewController : UIViewController <UIActionSheetDelegate>
-
- - (IBAction)changeAvata:(id)sender;
-
- @end
-
-
-
- - (IBAction)changeAvata:(id)sender {
-
- UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"更换头像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从相册选择", nil nil];
-
-
- actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
-
-
-
-
-
-
-
-
-
-
-
- NSString *num = [NSString stringWithFormat:@"%ld", actionSheet.numberOfButtons];
- NSLog(@"%@", num);
-
-
- NSString *btnTitle = [actionSheet buttonTitleAtIndex:1];
- NSLog(@"%@", btnTitle);
-
- [actionSheet showInView:self.view];
-
-
- }
-
- #pragma mark - UIActionSheetDelegate
-
- - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
- if (buttonIndex == 1) {
- NSLog(@"拍照");
- }
- else if (buttonIndex == 2) {
- NSLog(@"相册");
- }
- }
-
-
- - (void)actionSheetCancel:(UIActionSheet *)actionSheet {
-
- }
-
-
- - (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
-
- }
-
-
- -(void)didPresentActionSheet:(UIActionSheet *)actionSheet {
-
- }
-
-
- - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
-
- }
-
-
- - (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
-
- }
基本的使用方法就是上面这样啦,有其他的也希望能多多交流~
这里有我的工程源码https://github.com/Cloudox/UIActionSheetTest