iOS中的表单按钮选项UIActionSheet常用方法整理


什么是操作表单?看图:

一看图就明白了,毋需多说。

复制代码 代码如下:

UIActionSheet* mySheet = [[UIActionSheet alloc] 
                           initWithTitle:@"ActionChoose"  
                           delegate:self  
                           cancelButtonTitle:@"Cancel" 
                           destructiveButtonTitle:@"Destroy" 
                           otherButtonTitles:@"OK", nil]; 
    [mySheet showInView:self.view]; 

与UIAlertView类似,我们也是在委托方法里处理按下按钮后的动作。记得在所委托的类加上UIActionSheetDelegate。
复制代码 代码如下:

- (void)actionSheetCancel:(UIActionSheet *)actionSheet{ 
    // 

- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    // 

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{ 
    // 

-(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{ 
    // 


看到那个红色的按钮没?那是ActionSheet支持的一种所谓的销毁按钮,对某户的某个动作起到警示作用,
比如永久性删除一条消息或者日志。如果你指定了一个销毁按钮他就会以红色高亮显示:
复制代码 代码如下:

mySheet.destructiveButtonIndex=1; 

与导航栏类似,操作表单也支持三种风格 :
复制代码 代码如下:

UIActionSheetStyleDefault              //默认风格:灰色背景上显示白色文字 
UIActionSheetStyleBlackTranslucent     //透明黑色背景,白色文字 
UIActionSheetStyleBlackOpaque          //纯黑背景,白色文字 

用法用例:
复制代码 代码如下:

mySheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;

常用方法和属性

显示ActionSheet方法:
1.在一个视图内部显示,可以用showInView

复制代码 代码如下:

[mySheet showInView:self];

2.如果要将ActonSheet 与工具栏或者标签栏对齐,可以使用showFromToolBar或showFromTabBar
复制代码 代码如下:

[mySheet showFromToolBar:toolbar];
[mySheet showFromTabBar:tabbar];

解除操作表单
用户按下按钮之后,Actionsheet就会消失——除非应用程序有特殊原因,需要用户按下做个按钮。用dismiss方法可令表单消失:

复制代码 代码如下:

[mySheet dismissWithClickButtonIndex:1 animated:YES]; 
@property(nonatomic,copy) NSString *title;

设置标题

复制代码 代码如下:

@property(nonatomic) UIActionSheetStyle actionSheetStyle;

添加一个按钮,会返回按钮的索引

复制代码 代码如下:

- (NSInteger)addButtonWithTitle:(NSString *)title;

[/code]

获取按钮标题

复制代码 代码如下:

- (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;

点击按钮后,视图已经收回时触发的方法


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3