一、UIActionSheet的通常实现方法:
- (void)testActionSheetStatic {
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Static UIActionSheet"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Item A", @"Item B", @"Item C", nil];
[sheet showFromRect:view.bounds inView:view animated:YES];
[sheet release];
}
二、 如果事先知道各个按钮并且再也不会改变的情况下,这样的实现是OK的。但如果我要在运行时改变应该怎么办呢?动态添加按钮看起来应该也很简单,不要init函数中指定而在之后添加可以了,如下代码就展示了这点。
- (void)testActionSheetDynamic {
// 创建时仅指定取消按钮
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Dynamic UIActionSheet" delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:nil];
// 逐个添加按钮(比如可以是数组循环)
[sheet addButtonWithTitle:@"Item A"];
[sheet addButtonWithTitle:@"Item B"];
[sheet addButtonWithTitle:@"Item C"];
[sheet showFromRect:view.bounds inView:view animated:YES];
[sheet release];
}
运行下就发现问题很明显——取消按钮是在视图的顶部,而标准做法是显示在底部。怎么解决呢?如果在init函数中添加取消按钮就无法解决了。最后找到了一种将取消按钮也动态添加。
- (void)testActionSheetDynamic {
// 创建时不指定按钮
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];
}
这样取消按钮就显示在底部并且行为也符合预期了。
对我来说现在剩下的最大一个疑问就是destructive按钮到底是什么(Apple文档也没有清晰地说明这点)?一些实验结果也表明它实际上和 取消按钮并无区别,只不过它有一个红色背景而不是黑色的。所有如果在上例中改变destructiveButtonIndex而不是 cancelButtonIndex,就可以看到标有“取消”的按钮有红色背景了。
三、出于完整性的考虑,和上述代码相匹配的delegate代码如下:
- (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;
}
}
}
分享到:
相关推荐
Android 仿iPhone UIActionSheet控件效果很好用类似qq上传头像前的选择控件
一个改变UIActionSheet中按钮的例子,自己以前去试着这样做的时候,一直没成功,这个使用帮了我大忙啊~
主要介绍了iOS中UIActionSheet动态添加按钮功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下
UIActionSheet demo23456789
UIActionSheet示例代码,IOS 开发总结
ios应用源码之仿uialertview和uiactionsheet的一个效果
IOS自定义UIActionSheet的例子,供大家学习
ActionSheetPicker = UIPickerView + UIActionSheet
高仿微信ActionSheet,使用和UIActionSheet基本相同,代码有不足,仅供参考。
iPhone开发之UIActionSheet
iPhone开发之UIActionSheet
iOS程序中的Action Sheet就像Windows中的 “确定-取消”对话框一样,用于强制用户进行选择。当用户将要进行的操作具有一定危险时,常常使用Action Sheet对用户进行危险提示,这样,用户有机会进行取消操作。...
多功能选择器UIActionSheet,仿微信ActionSheet,还有其他样式改变,满足各种需求
IOS应用源码之仿UIAlertView和UIActionSheet的一个效果 .rar
BlockAlertsAnd-ActionSheets, 出色的UIAlertView和UIActionSheet替换灵感由 TweetBot 它是如何启动的: A的故事。自从我第一次在应用中使用UIActionSheet或者UIAlertView时,我不... 我也不喜欢在按钮的事件中应该执
DoActionSheet, UIActionSheet的替换 DoActionSheet替代 UIActionSheet: 基于块,可以定制主题,易于使用图像或者地图预览带有标题,带有破坏性按钮的,带有取消按钮和图像 如果按钮有许多按钮但按钮的位置是固定的...
苹果ios开发100个实例源码; 个人信息输入表(支持表单增删) 二维码扫描应用demo 二维码生成器 从GIF动画创建一个动态UIImages 对象 从上往下拉出现加载view,动态加载内容 从上往下拉动态加载 从上往下拉动态加载3 ...
简单的自定义UIActionSheet
将封装的UIActionSheet改为UIView,为了多态初始化时添加了枚举(几个时间选择器样式)。 UIActionSheet改为UIView核心代码为以下几个方法: - (void)showInView:(UIView *)view; // UIPicker显示 -(void)...