---
title: " UIButton中的UIActionSheet操作表参数解析"
author: "Davidream"
date: "2012-05-11T10:44:17Z"
tags: []
categories: ["移动开发"]
slug: "uibutton-uiactionsheet"
draft: false
lastmod: "2012-05-11T10:44:17Z"
id: 1522701
url: "https://davidream.iteye.com/blog/1522701"
---

在ViewController.xib文件中添加一个UIButton按钮，然后我们实现一个提示功能窗口：

相关方法实现代码如下：

 

**\[plain]** [view plain](http://blog.csdn.net/yangbo_hbzjk/article/details/7556087 "view plain")[copy](http://blog.csdn.net/yangbo_hbzjk/article/details/7556087 "copy")

1. -(IBAction)doSomething:(id)sender
2. {
3. UIActionSheet \*actionSheet =\[\[UIActionSheet alloc]initWithTitle:@"Are you sure?" delegate:self cancelButtonTitle:@"No Way" destructiveButtonTitle:@"yes,I'm Sure" otherButtonTitles:nil];
4. \[actionSheet showInView:self.view];
5. \[actionSheet release];
6. }

doSomething方法的作用是什么呢？我们首先分配并初始化一个UIActionSheet对象，该对象表示一个操作表：

 

初始化方法接受了五个参数，我们来说一下参数含义：

       第一个参数：initWithTitle：是显示的标题，显示在操作表的顶部。

       第二个参数：delegate：是操作表的委托，将在该表上的按钮被按下时收到通知，更确切地说，委托的ActionSheet：didDismissWithButtonIndex:actionSheet:didDismisswithButtonIndex:方法将被调用。通过将self作为委托参数传递给该方法，我们可以确保本程序的actionSheet:didDismisswithButtonIndex:方法将被调用。

       cancelButtonTitle：是取消按钮的标题，用户可以通过点击此按钮表明不希望继续操作。所有操作表都应该有一个取消按钮，但你可以根据需要为它们指定合适的标题。如果没有选择，则不必使用操作表。如果只希望通知用户，但不让用户做出选择，那么用通知比较合适。

       destructiveButtonTitle:确定按钮的标题，同上相反。

       otherButtonTitle：是用于指定希望在表单上现实的其他按钮的数量，该参数可以使用各种值，这是OC语言中的特性，如果我们希望操作表上有另外两个按钮，可以写如下代码：

 

**\[plain]** [view plain](http://blog.csdn.net/yangbo_hbzjk/article/details/7556087 "view plain")[copy](http://blog.csdn.net/yangbo_hbzjk/article/details/7556087 "copy")

1. otherButtonTitle:@"Foo",@"Bar",nil;

**\[plain]** [view plain](http://blog.csdn.net/yangbo_hbzjk/article/details/7556087 "view plain")[copy](http://blog.csdn.net/yangbo_hbzjk/article/details/7556087 "copy")

1. 这样操作表将提供四个按钮供用户选择，可以在otherButtonTitles参数中传递任意数量的变量，只要nil作为最后一个变量传递即可。但根据可用屏幕空间的大小，按钮的数量将收到实际限制。

**\[plain]** [view plain](http://blog.csdn.net/yangbo_hbzjk/article/details/7556087 "view plain")[copy](http://blog.csdn.net/yangbo_hbzjk/article/details/7556087 "copy")

1. 创建操作表后，操作表始终有一个父视图，即当前对用户可见的视图。在本例中，我们希望使用在Interface Builder中设计的视图作为父视图，因此使用self.view。view是父类UIViewController的一个属性，指向该类的控制器对应的视图。

**\[plain]** [view plain](http://blog.csdn.net/yangbo_hbzjk/article/details/7556087 "view plain")[copy](http://blog.csdn.net/yangbo_hbzjk/article/details/7556087 "copy")