iOS中UIActionSheet的使用
来源:互联网 发布:非递归后序遍历c语言 编辑:程序博客网 时间:2024/05/16 15:33
UIActionSheet是在iOS弹出的选择按钮项,可以添加多项,并为每项添加点击事件。
为了快速完成这例子,我们打开Xcode 4.3.2, 先建立一个single viewapplication。然后再xib文件添加一个button,用来弹出sheet view。
1、首先在.h文件中实现协议
加代码的地方在@interface那行的最后添加,协议相当于java里的接口,实现协议里的方法。
- @interface
sheetviewViewController : UIViewController -
- @end
2、添加button,命名button为showSheetView.
3、为button建立Action映射,映射到.h文件上,事件类型为Action,命名为showSheet。
4、在.m文件上添加点击事件代码
图的效果是这样的:
- -
(IBAction)showSheet:(id)sender { -
UIActionSheet *actionSheet = [[UIActionSheet alloc] -
initWithTitle:@"title,nil时不显示" -
delegate:self -
cancelButtonTitle:@"取消" -
destructiveButtonTitle:@"确定" -
otherButtonTitles:@"第一项", @ "第二项",nil]; -
actionSheet.actionSheetStyle = UIActionSheetStyleBlackO paque; -
[actionSheet showInView:self.view]; - }
actionSheet.actionSheetStyle
参数解释:
cancelButtonTitle
otherButtonTitles是自己定义的项,注意,最后一个参数要是nil。
[actionSheet
对应上面的图和代码,一目了然了把
5、接下来我们怎么相应ActionSheet的选项的事件呢?
实现协议里的方法。为了能看出点击Actionsheet每一项的效果,我们加入UIAlertView来做信息显示。下面是封装的一个方法,传入对应的信息,在UIAlertView显示对应的信息。
- -(void)showAlert:(NSString
*)msg { -
UIAlertView *alert = [[UIAlertView alloc] -
initWithTitle:@"Action Sheet选择项" -
message:msg -
delegate:self -
cancelButtonTitle:@"确定" -
otherButtonTitles: nil]; -
[alert show]; - }
那相应被Action Sheet选项执行的代码如下:
- (void)actionSheet:(UIActionSheet
*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex - {
-
if (buttonIndex == 0) { -
[self showAlert:@"确定"]; -
}else if (buttonIndex == 1) { -
[self showAlert:@"第一项"]; -
}else if(buttonIndex == 2) { -
[self showAlert:@"第二项"]; -
}else if(buttonIndex == 3) { -
[self showAlert:@"取消"]; -
} -
- }
- -
(void)actionSheetCancel:(UIActionSheet *)actionSheet{ -
- }
- -(void)actionSheet:(UIActionSheet
*)actionSheet didDismissWithButtonInde x:(NSInteger)buttonIndex{ -
- }
- -(void)actionSheet:(UIActionSheet
*)actionSheet willDismissWithButtonInd ex:(NSInteger)buttonIndex{ -
- }
看到那个红色的按钮没?那是ActionSheet支持的一种所谓的销毁按钮,对某户的某个动作起到警示作用,
比如永久性删除一条消息或图像时。如果你指定了一个销毁按钮他就会以红色高亮显示:
actionSheet.destructiveButtonIndex=1;
与导航栏类似,操作表单也支持三种风格
UIActionSheetStyleDefaul
UIActionSheetStyleBlackT
UIActionSheetStyleBlackO
用法:
我选sheet 里的第一项,显示如下:
6、注意事项
在开发过程中,发现有时候UIActionSheet的最后一项点击失效,点最后一项的上半区域时有效,这是在特定情况下才会发生,这个场景就是使用了UITabBar的时候才有。
解决办法:
在showView时这样使用,[actionSheet showInView:[UIApplicationsharedApplication].keyWindow];
或者[sheet showInView:[AppDelegatesharedDelegate].tabBarController.view];
这样就不会发生遮挡现象了。
- iOS中UIActionSheet的使用
- iOS中UIActionSheet的使用
- iOS中UIActionSheet的使用
- iOS UIActionSheet的使用
- ios--UIActionSheet的使用
- ios-UIActionSheet的使用
- [IOS]UIActionSheet的使用
- [IOS]UIActionSheet的使用
- IOS中UIActionSheet使用详解
- iOS中UIActionSheet使用详解
- IOS中UIActionSheet使用详解
- ios中UIActionSheet的误区
- iOS之UIActionSheet的使用
- swift中UIActionSheet的使用
- swift中UIActionSheet的使用
- swift中UIActionSheet的使用
- iOS学习之UIActionSheet的使用
- IOS学习——UIActionSheet的使用
- 自定义默认生成的导航栏 标题 颜色…
- iOS UIlabel 自动换行,文字自适应
- iOS中NSNotificationCenter…
- IOS小知识
- ios开发 UITableCell 可重用问题
- iOS中UIActionSheet的使用
- 使用Settings Bundle为程序添加设…
- 教程01.利用终端创建文件
- JVM垃圾收集器和内存分配侧策略
- cocos2d-x:02创建动画
- 效果图制作
- 03:把文字和图片加入图层
- Cocos-2d中各个类之间的结构…
- Cocos2d中几种常用类简介