UIActionSheet

(文章转载自:http://blog.csdn.net/shenjx1225/article/details/8709983

 

【iOS学习】----UIActionSheet

标签: UIActionSheet
 分类:

创建:

 

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Hello"  
  2.                                                   delegate:self  
  3.                                          cancelButtonTitle:@"Cancel"  
  4.                                     destructiveButtonTitle:@"OK"  
  5.                                          otherButtonTitles:nil];  
  6. actionSheet.tag = TAG_ACTIONSHEET;  
  7. actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;  

 tag:标签。

 

 actionSheetStyle:actionSheet的外观样式。有三种:

        1.UIBarStyleDefault(默认,黑色);

       2.UIBarStyleBlackTranslucent(黑色半透明);

       3.UIBarStyleBlackOpaque(黑色不透明)。

delegate:代理。最常用的是实现下面这个代理方法(注释如下):

 

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. // Called when a button is clicked. The view will be automatically dismissed after this call returns  
  2. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;  
显示

 

 

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. [actionSheet showInView:self.view];  

显示actionSheet有如下几个函数:

 

1.- (void)showFromTabBar:(UITabBar *)view1

从指定的tabbar来呈现actionSheet。

在ipad上,这个方法把actionSheet居中在屏幕的正中心。如果你想在ipad上呈现这个actionSheet相对于tabbar的位置,你可以使用 showFromRect:inView:animated: 这个方法。

2.- (void)showFromToolbar:(UIToolbar *)view

类似于第一个方法,就不多说了。

3.- (void)showInView:(UIView *)view

从指定的视图来呈现actionSheet。

在ipad上,这个方法把actionSheet居中在屏幕的正中心。如果你想在ipad上呈现这个actionSheet,你可以使用 showFromRect:inView:animated: 这个方法。

4.- (void)showFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated

 

一般在ipad上用这个方法.

在iPad中,不应在视图上显示操作表。Apple用户界面指南指出,必须在弹出框中显示操作表。弹出框(popover)是一种独特的用户界面元素,在用户触摸某个屏幕元素时出现,并通常在用户触摸背景时候消失。弹出框还包含一个小箭头,指向触发它的UI元素。为符合Apple的要求,即操作表必须显示在弹出框中,可使用方法showFromRect:inView:animated来显示一个包含操作表的弹出框。其中第一个参数(showFromRect)指定了屏幕上的一个矩形区域,弹出框的箭头将指向它。为正确设置该参数,可使用sender变量(这里是一个UIButton对象)的frame属性。参数inView指定了操作表/弹出框将显示在其中的视图(这里设置为self.view)。而参数animated是一个布尔值,指定是否以动画方式显示。

当您在iPad上运行该应用程序时,操作表将包含在一个弹出框中;而在iPhone上运行时,将忽略额外的参数,就像使用方法showInView那样显示操作表。

对于操作表,需要指出的最后一点是,当您在iPad上弹出框中显示操作表时,将自动忽略取消按钮。这是因为触摸弹出框的外面与单击取消按钮的效果相同,因此这个按钮是多余的。


5.- (void)showFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated
点击item,弹出actionSheet,箭头指向item,与方法4类似。效果如下图:



posted @ 2016-01-18 14:06  我叫南部21号  阅读(114)  评论(0)    收藏  举报