UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"actionSheet 实例?"
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:@"确定"
otherButtonTitles:@"其他按钮",nil];
[actionSheet showInView:[self view]];
这样会生成一个有三个按钮的actionSheet表单,确定按钮,其他,取消按钮,分别的按钮索引为0,1,2.这个索引可以会被委托方法使用。
还可以设置一下此actionSheet的一些特性:
actionSheet.destructiveButtonIndex=0;//设置那个红色的提醒按钮是哪一个,默认是上面的确定按钮
actionSheet.tag = TAG_MY_ACTIONSHEET;//设置actionSheet的tag标记,便于在同一个delegate方法区分不同的actionSheet
actionSheet.actionSheetStyle =UIActionSheetStyleDefault;//设置不同的actionSheet风格,此为灰色背景,白字。建议自己测试一下想要的风格
有几个delegate方法,最常用的肯定是按下按钮的了:#pragma mark -
#pragma mark actionSheet delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
NSLog(@"click at index %d,确定操作", buttonIndex);
break;
case 1:
NSLog(@"click at index %d,其他操作", buttonIndex);
break;
case 2:
NSLog(@"click at index %d,取消操作", buttonIndex);
break;
default:
NSLog(@"unknown: click at index %d", buttonIndex);
break;
}
}
}
实际上还有其他的一些委托方法,我没有测试,就没写,需要自己去测试了。
上面的actionSheet可能会有按钮按的时候不容易按下。转自:http://blog.csdn.net/zcl369369/article/details/7515069
如图,只有在点标出的红框才行,点别的地方都没反应,在模拟器和真机上都是这样
遇到过同样的问题
你调用– showInView: 时传入的View 有问题,那个View的区域就不包括底下的TabBarController
你的 view size 不够. 或者你显示的方式不对. 比如你页面下方有一个 tabbar, 但是你显示是 showInView 有可能会出现这中情况, 改为 showFromTabbar .
恩,是被tabbar挡住了
解决:[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
取消按钮位置设定,自己没有测试,方便自己以后用的时候。转载自:http://blog.csdn.net/qhexin/article/details/7066341
使用 UIActionsheet 的时候,如果otherButton太多的话,取消按钮会排版到最下面,如果otherButton很少的话,取消按钮又排版在上面。
这样点击按钮时我们收到的按钮index 不是很确定。
所以干脆就让取消按钮一直在下面。
代码如下:
// 创建时不指定按钮
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Dynamic UIActionSheet" delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
// 逐个添加按钮(比如可以是数组循环)
[sheet addButtonWithTitle:@"Item A"];
[sheet addButtonWithTitle:@"Item B"];
[sheet addButtonWithTitle:@"Item C"];
// 同时添加一个取消按钮
[sheet addButtonWithTitle:@"Cancel"];
// 将取消按钮的index设置成我们刚添加的那个按钮,这样在delegate中就可以知道是那个按钮
sheet.cancelButtonIndex = sheet.numberOfButtons-1;
[sheet showFromRect:view.bounds inView:view animated:YES];
[sheet release];
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == actionSheet.cancelButtonIndex)
{ return; }
switch (buttonIndex)
{
case 0: {
NSLog(@"Item A Selected");
break;
}
case 1: {
NSLog(@"Item B Selected");
break;
}
case 2: {
NSLog(@"Item C Selected");
break;
}
}
}