iOS组件之UIActionSheet详解
引言
UIActionSheet继承于UIView(有关UIView的介绍请到《iOS组件之UIView详解》)。使用UIActionSheet类有一组关于如何进行给定任务的备选方案呈现给用户。也可以使用动作表,以提示用户进行确认,有潜在危险的动作。动作片包含一个可选的标题和一个或多个按钮,其中每一个对应于要采取的动作。
目录
// 初始化UIActionSheet上的button按钮
- (id)initWithTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... ;
// 设置UIActionSheet代理
id<UIActionSheetDelegate> delegate; // weak reference
// 设置UIActionSheet的title
NSString *title;
// 设置UIActionSheet的样式(默认为UIActionSheetStyleAutomatic)
UIActionSheetStyle actionSheetStyle;
// 通过按钮的title获取这个按钮在UIActionSheet上的下标
- (NSInteger)addButtonWithTitle:(NSString *)title;
// 通过下标获得按钮的title
- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;
// 获取按钮的总个数(只读)
NSInteger numberOfButtons;
// 获取取消按钮的下标
NSInteger cancelButtonIndex;
// 红色按钮的下标(翻译成破坏性的,其实就是那个红色的按钮)
NSInteger destructiveButtonIndex;
// 获取第一个其他按钮的下标(只读)
NSInteger firstOtherButtonIndex;
// UIActionSheet是否可见(只读)
BOOL visible;
// show a sheet animated. you can specify either a toolbar, a tab bar, a bar butto item or a plain view. We do a special animation if the sheet rises from // a toolbar, tab bar or bar button item and we will automatically select the correct style based on the bar style. if not from a bar, we use // UIActionSheetStyleDefault if automatic style set // 如果要将ActonSheet 与工具栏或者标签栏对齐,可以使用showFromToolBar(showFromTabBar)
- (void)showFromToolbar:(UIToolbar *)view;
- (void)showFromTabBar:(UITabBar *)view;
// showFromBarButtonItem,就是popover会指向个BarButtonItem。在iPhone上只能使用showInView,如果是universal app,要用些if来判断。用delegate来知道用户选择了什么
- (void)showFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated;
// showFromRect,在ipad上指定个矩形区域,它会在其上显示个popover
- (void)showFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated;
// 显示在iphone的中间位置,它会从下往上滑动;在ipad上它会放在中间,所以在ipad上从不用showInView
- (void)showInView:(UIView *)view;
// hides alert sheet or popup. use this method when you need to explicitly dismiss the alert. // it does not need to be called if the user presses on a button
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;
UIActionSheetDelegate协议(可选)
// 点击UIActionSheet会调用的函数,buttonIndex为所点击button在UIActionSheet的下标
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
// 点击取消按钮会调用的函数
- (void)actionSheetCancel:(UIActionSheet *)actionSheet;
// 将来显示ActionSheet
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet;
// 显示ActionSheet
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet;
// ActionSheet将要消失
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex;
// ActionSheet消失
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;