以下代码是自己为了测试而写的,不是转的别人的代码。但是由于下面的很多东西都是看的别人的一些博客汇总而成,故列为转载,于是就不写转载自哪里了。
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"actionSheet 实例?"
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:@"确定"
otherButtonTitles:@"其他按钮",nil];
[actionSheet showInView:[self view]];
[actionSheet release];
这样会生成一个有三个按钮的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];
我的是有一个tabbar,我用的办法:[alertshowFromTabBar:self.tabBarController.tabBar];
取消按钮位置设定,自己没有测试,方便自己以后用的时候。转载自:http://blog.csdn.net/qhexin/article/details/7066341
使用 UIActionsheet 的时候,如果otherButton太多的话,取消按钮会排版到最下面,如果otherButton很少的话,取消按钮又排版在上面。
这样点击按钮时我们收到的按钮index 不是很确定。
所以干脆就让取消按钮一直在下面。
代码如下:
- <pre style="color: rgb(102, 102, 102); font-size: 14px; background-color: rgb(221, 221, 221); ">// 创建时不指定按钮
- 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];</pre>
- <pre></pre>
- <p></p>
- <pre></pre>
- <p></p>
- <p></p>
- <p></p><pre name="code" class="java">- (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;
- }
- }
- }</pre><br>
- <br>
- <p></p>
- <p></p>
- <p><br>
- </p>
-
- <pre></pre>
UIActionSheet与UIAlertView 详解
OS程序中的Action Sheet就像Windows中的 “确定-取消”对话框一样,用于强制用户进行选择。当用户将要进行的操作具有一定危险时,常常使用Action Sheet对用户进行危险提示,这样,用户有机会进行取消操作。
Alert相当于Windows中的Messagebox,跟Action Sheet也是类似的。不同的是,Alert可以只有一个选择项,而Action Sheet却至少要两个选项。
- (IBAction)buttonPressed:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"Are you sure?"
delegate:self
cancelButtonTitle:@"No Way!"
destructiveButtonTitle:@"Yes, I'm sure!"
otherButtonTitles:nil];
[actionSheet showInView:self.view];
}
(1)initWithTitle:设置标题,将会显示在Action Sheet的顶部
(2)delegate:设置Action Sheet的委托。当Action Sheet的一个按钮被按下后,它的delegate将会被通知,并且会执行这个delegate的actionSheet: didDismissWithButtonIndex方法将会执行。这里,我们将delegate设成self,这样可以保证执行我们自己在ViewController.m写的actionSheet: didDismissWithButtonIndex方法
(3)cancelButtonTitle:设置取消按钮的标题,这个取消按钮将会显示在Action Sheet的最下边
(4)destructiveButtonTitle:设置第一个确定按钮的标题,这个按钮可以理解成:"好的,继续"
(5)otherButtonTitles:可以设置任意多的确定按钮,想要添加两个按钮,可以写成:
otherButtonTitles: @”New Button 1”, @”New Button 2”, nil
[actionSheet showInView:self.view]这条语句用来显示Action Sheet,准确的说,这条语句是给这个Action Sheet设置Parent,而这个Parent必须是一个View,并且是当前正在显示的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];
-------------------------------------------------------------------------------------------------
http://oo-life.com/post/2012-07-04/40030501636
UIAlertView 详解
构造一个Alert也要填写很多参数:
msg = @"You can breathe easy, everything went OK.";
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Something was done"
message:msg
delegate:self
cancelButtonTitle:@"Prew!"
otherButtonTitles: nil];
[alert show];
(1)initWithTitle:设置标题,将会显示在Alert的顶部
(2)message:设置提示消息内容
(3)delegate:设置Alert的委托。这里,我们设成self
(4)cancelButtonTitle:设置取消按钮的标题
(5)otherButtonTitles:与Action Sheet类似
[alert show]这条语句用来显示Alert。
OS5中UIAlertView新增了一个属性alertViewStyle,它的类型是UIAlertViewStyle,是一个枚举值:
typedefenum{
UIAlertViewStyleDefault= 0, UIAlertViewStyleSecureTextInput, UIAlertViewStylePlainTextInput, UIAlertViewStyleLoginAndPasswordInput } UIAlertViewStyle;
alertViewStyle属性默认是UIAlertViewStyleDefault
我们可以把它设置为UIAlertViewStylePlainTextInput,那么AlertView就显示为这样:
UIAlertViewStyleSecureTextInput显示为:
UIAlertViewStyleLoginAndPasswordInput为:
iOS5中同时写新增了一个方法来获取这些TextField控件对象:
- (UITextField*)textFieldAtIndex:(NSInteger)textFieldIndex
UIAlertViewStyleSecureTextInput和UIAlertViewStylePlainTextInput可以通过textFieldIndex为0来获取输入框对象。UIAlertViewStyleLoginAndPasswordInput可以通过textFieldIndex为0和1分别获取用户名输入框对象和密码输入框对象。
其他函数说明
// it does not need to be called if the user presses on a button
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;
// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
- (void)willPresentAlertView:(UIAlertView *)alertView; // before animation and showing view
- (void)didPresentAlertView:(UIAlertView *)alertView; // after animation
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex; // before animation and hiding view
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex; // after animation
----------------------------------
UIActionSheet的使用 (转)
IActionSheet是在IOS弹出的选择按钮项,可以添加多项,并为每项添加点击事件。
为了快速完成这例子,我们打开Xcode 4.3.2, 先建立一个single view application。然后再xib文件添加一个button,用来弹出sheet view。
1、首先在.h文件中实现协议,加代码的地方在@interface那行的最后添加<UIActionSheetDelegate>,协议相当于java里的接口,实现协议里的方法。
2、添加button,命名button为showSheetView.
3、为button建立Action映射,映射到.h文件上,事件类型为Action ,命名为showSheet。
4、在.m文件上添加点击事件代码
图的效果是这样的:
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;//设置样式
参数解释:
cancelButtonTitle destructiveButtonTitle是系统自动的两项。
otherButtonTitles是自己定义的项,注意,最后一个参数要是nil。
[actionSheet showInView:self.view];这行语句的意思是在当前view显示Action sheet。当然还可以用其他方法显示Action sheet。
对应上面的图和代码,一目了然了把
5、接下来我们怎么相应Action Sheet的选项的事件呢?实现协议里的方法。为了能看出点击Action sheet每一项的效果,我们加入UIAlertView来做信息显示。下面是封装的一个方法,传入对应的信息,在UIAlertView显示对应的信息。
那相应被Action Sheet选项执行的代码如下:
可以看到 buttonIndex 是对应的项的索引。
看到那个红色的按钮没?那是ActionSheet支持的一种所谓的销毁按钮,对某户的某个动作起到警示作用,
比如永久性删除一条消息或图像时。如果你指定了一个销毁按钮他就会以红色高亮显示:
actionSheet.destructiveButtonIndex=1;
与导航栏类似,操作表单也支持三种风格 :
UIActionSheetStyleDefault //默认风格:灰色背景上显示白色文字
UIActionSheetStyleBlackTranslucent //透明黑色背景,白色文字
UIActionSheetStyleBlackOpaque //纯黑背景,白色文字
用法:
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;//设置样式
我选sheet 里的第一项,显示如下:
6、注意事项,在开发过程中,发现有时候UIActionSheet的最后一项点击失效,点最后一项的上半区域时有效,这是在特定情况下才会发生,这个场景就是试用了UITabBar的时候才有。解决办法:
在showView时这样使用,[actionSheet showInView:[UIApplication sharedApplication].keyWindow];或者[sheet showInView:[AppDelegate sharedDelegate].tabBarController.view];这样就不会发生遮挡现象了。