代码改变世界

iOS UI基础-6.0 UIActionSheet的使用

  jiangys  阅读(393)  评论(0编辑  收藏  举报

UIActionSheet是在iOS弹出的选择按钮项,可以添加多项,并为每项添加点击事件.

使用

1.需要实现UIActionSheetDelegate  协议

@interface NJWisdomCardDetailViewController ()<UIActionSheetDelegate>

@end

2.弹出选择按钮框

复制代码
- (void)showSheet{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"title,nil时不显示"
                                  delegate:self
                                  cancelButtonTitle:@"取消"
                                  destructiveButtonTitle:@"确定"
                                  otherButtonTitles:@"第一项", @"第二项",nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];
}
复制代码

参数解释: 

  • actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;//设置样式
  • cancelButtonTitle和destructiveButtonTitle 是系统带的两个按钮。
  • otherButtonTitles是自己定义的项,注意,最后一个参数要是nil。
  • [actionSheet showInView:self.view]; 这行语句的意思是在当前view显示Action sheet。

设置样式,操作表单也支持三种风格:

  • UIActionSheetStyleDefault              //默认风格:灰色背景上显示白色文字   
  • UIActionSheetStyleBlackTranslucent     //透明黑色背景,白色文字   
  • UIActionSheetStyleBlackOpaque          //纯黑背景,白色文字  

3.监听项的点击事件。实现协议里的有相应的方法

复制代码
(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 didDismissWithButtonIndex:(NSInteger)buttonIndex{  

}  
-(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{  

}
复制代码

注意事项

在开发过程中,发现有时候UIActionSheet的最后一项点击失效,点最后一项的上半区域时有效,这是在特定情况下才会发生,这个场景就是试用了UITabBar的时候才有。解决办法:

在showView时这样使用,[actionSheet showInView:[UIApplication sharedApplication].keyWindow];或者[sheet showInView:[AppDelegate sharedDelegate].tabBarController.view];这样就不会发生遮挡现象了。

 

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
历史上的今天:
2014-09-14 jquery.lazyload 使用
点击右上角即可分享
微信分享提示