UIActionSheet操作表单-iOS开发


什么是操作表单?看图:

一看图就明白了,毋需多说。
  1. UIActionSheet* mySheet = [[UIActionSheet alloc]  
  2.                            initWithTitle:@"ActionChoose"   
  3.                            delegate:self   
  4.                            cancelButtonTitle:@"Cancel"  
  5.                            destructiveButtonTitle:@"Destroy"  
  6.                            otherButtonTitles:@"OK", nil];  
  7.     [mySheet showInView:self.view];  

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

  1. - (void)actionSheetCancel:(UIActionSheet *)actionSheet{  
  2.     //   
  3. }  
  4. - (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{  
  5.     //   
  6. }  
  7. -(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{  
  8.     //   
  9. }  
  10. -(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{  
  11.     //   
  12. }  

看到那个红色的按钮没?那是ActionSheet支持的一种所谓的销毁按钮,对某户的某个动作起到警示作用,

比如永久性删除一条消息或者日志。如果你指定了一个销毁按钮他就会以红色高亮显示:

  1. mySheet.destructiveButtonIndex=1;  
与导航栏类似,操作表单也支持三种风格 :
  1. UIActionSheetStyleDefault              //默认风格:灰色背景上显示白色文字   
  2. UIActionSheetStyleBlackTranslucent     //透明黑色背景,白色文字   
  3. UIActionSheetStyleBlackOpaque          //纯黑背景,白色文字  
用法用例:

mySheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;

显示ActionSheet有三种方法:

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

[mySheet showInView:self];

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

[mySheet showFromToolBar:toolbar];

[mySheet showFromTabBar:tabbar];
解除操作表单

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

  1. [mySheet dismissWithClickButtonIndex:1 animated:YES];  

相关内容