OS中UIActionSheet使用方法详解
是个弹出按钮
一、初始化方法
- (instancetype)initWithTitle:(NSString *)title delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...;
// 方法1 无代理,只有2个确定按钮
UIActionSheet *actionsheet01 = [[UIActionSheet alloc] initWithTitle:@"按钮点击后我才出现的。" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"知道了", nil];
// 显示
[actionsheet01 showInView:self.view];
// 方法2 无代理,有多个确定按钮
UIActionSheet *actionsheet02 = [[UIActionSheet alloc] initWithTitle:@"按钮点击后我才出现的。" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"知道了0", @"知道了1", @"知道了2", @"知道了3", nil];
// 显示
[actionsheet02 showInView:self.view];
// 方法3 有代理,有2个确定按钮
/*
1 设置代理为 self
2 添加协议
3 实现方法
*/
UIActionSheet *actionsheet03 = [[UIActionSheet alloc] initWithTitle:@"选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册", @"拍照", nil];
// 显示
[actionsheet03 showInView:self.view];
// 添加协议
@interface ViewController () <UIActionSheetDelegate>
@end
// UIActionSheetDelegate实现代理方法
-
(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"buttonIndex=%ld", buttonIndex);// 方法1
// if (0 == buttonIndex)
// {
// NSLog(@"点击了相册按钮");
// }
// else if (1 == buttonIndex)
// {
// NSLog(@"点击了拍照按钮");
// }
// else if (2 == buttonIndex)
// {
// NSLog(@"点击了取消按钮");
// }
参数说明:
title:视图标题
delegate:设置代理
cancelButtonTitle:取消按钮的标题
destructiveButtonTitle:特殊标记的按钮的标题
otherButtonTitles:其他按钮的标题
二、常用方法和属性介绍
@property(nonatomic,copy) NSString *title;
设置标题
@property(nonatomic) UIActionSheetStyle actionSheetStyle;
设置风格,枚举如下:
typedef NS_ENUM(NSInteger, UIActionSheetStyle) {
UIActionSheetStyleAutomatic = -1,
UIActionSheetStyleDefault = UIBarStyleDefault,
UIActionSheetStyleBlackTranslucent = UIBarStyleBlackTranslucent,
UIActionSheetStyleBlackOpaque = UIBarStyleBlackOpaque,
};
- (NSInteger)addButtonWithTitle:(NSString *)title;
添加一个按钮,会返回按钮的索引
- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;
获取按钮标题
@property(nonatomic,readonly) NSInteger numberOfButtons;
获取按钮数量
@property(nonatomic) NSInteger cancelButtonIndex;
设置取消按钮的索引值
@property(nonatomic) NSInteger destructiveButtonIndex;
设置特殊标记
@property(nonatomic,readonly,getter=isVisible) BOOL visible;
视图当前是否可见
下面是几种弹出方式,会根据风格不同展现不同的方式
-
(void)showFromToolbar:(UIToolbar *)view;
-
(void)showFromTabBar:(UITabBar *)view;
-
(void)showFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated ;
-
(void)showFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated ;
-
(void)showInView:(UIView *)view;
-
(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;
使用代码将视图收回
三、UIActionSheet代理方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
点击按钮时触发的方法
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet;
视图将要弹出时触发的方法
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet;
视图已经弹出式触发的方法
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex;
点击按钮后,视图将要收回时触发的方法
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;
点击按钮后,视图已经收回时触发的方法
网友评论