您的位置:首页 > 产品设计 > UI/UE

ios中UIActionSheet的误区

2014-05-07 16:46 357 查看
今天在研究UIActionSheet 直接把代码放到viewDidLoad中来执行,费了半天的劲总是出现问题,也怀疑过是不是xcode的问题,后来发现平时用都是放到一个button的方法里来操作,于是有个观点产生UIActionSheet必须配合动作时才有效果。于是去查看开发文档,上面有句话也验证了观点:Action sheets display a set of buttons
representing several alternative choices to complete a task initiated by the user.

官方文档:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/UIKitUICatalog/UIActionSheet.html

代码:

@interface sheetviewViewController : UIViewController<UIActionSheetDelegate>

@end

UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"first ActionSheet" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除" otherButtonTitles:@"保持", nil];

actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;

[actionSheet showInView:self.view];

#pragma mark---实现UIActionSheetDelegate协议

- (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:@"取消"];

}

NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];

NSLog(@"buttonTitle = %@",buttonTitle);

}

actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;//设置样式
参数解释:
cancelButtonTitle destructiveButtonTitle是系统自动的两项。
otherButtonTitles是自己定义的项,注意,最后一个参数要是nil。

[actionSheet showInView:self.view];这行语句的意思是在当前view显示Action sheet。当然还可以用其他方法显示Action sheet。

可以看到 buttonIndex 是对应的项的索引。

看到那个红色的按钮没?那是ActionSheet支持的一种所谓的销毁按钮,对某户的某个动作起到警示作用,

比如永久性删除一条消息或图像时。如果你指定了一个销毁按钮他就会以红色高亮显示:

actionSheet.destructiveButtonIndex=1;
与导航栏类似,操作表单也支持三种风格 :

UIActionSheetStyleDefault //默认风格:灰色背景上显示白色文字

UIActionSheetStyleBlackTranslucent //透明黑色背景,白色文字

UIActionSheetStyleBlackOpaque //纯黑背景,白色文字
用法:
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;//设置样式
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: