UI基础 - UIActionSheet | 已弃用 |
■ 简言
1. UIActionSheet 是一个菜单式的界面,为用户提供操作命令选项!其界面是从屏幕底部向上弹出的,是系统自带的模态视图。自 iOS 8.3 开始弃用,由 UIAlertController 所代替
■ 使用方式
1 #import "ViewController.h" 2 @interface ViewController ()<UIActionSheetDelegate> // 遵守协议 3 @property(strong, nonatomic)UIActionSheet *actionSheet; 4 @end 5 6 @implementation ViewController 7 8 - (void)viewDidLoad { 9 [super viewDidLoad]; 10 self.view.backgroundColor = [UIColor yellowColor]; 11 12 // 确定、第一页、第二页、第三页 的下标依次 0、1、2、3 13 // 取消 的下标是 4 14 self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"提示信息" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"第一页", @"第二页",@"第三页",nil]; 15 self.actionSheet.actionSheetStyle = UIActionSheetStyleDefault;// 样式 16 BOOL res = self.actionSheet.visible;// 是否可见,是只读属性 17 self.actionSheet.title = @"提示"; // 修改标题 18 self.actionSheet.delegate = self; // 代理 19 20 // 添加按钮:其下标从 取消 的下标开始计数 21 [self.actionSheet addButtonWithTitle:@"第四页"]; // 下标是 5 22 [self.actionSheet addButtonWithTitle:@"第五页"]; 23 NSLog(@"%ld",[self.actionSheet numberOfButtons]); // 按钮总数 24 NSLog(@"%@",[self.actionSheet buttonTitleAtIndex:4]); // 获取标题 25 26 27 NSLog(@"%@",[self.actionSheet buttonTitleAtIndex:6]); // 获取 第五页 的标题 28 NSLog(@"%ld",[self.actionSheet cancelButtonIndex]); // 获取 取消 的下标 29 NSLog(@"%ld",[self.actionSheet destructiveButtonIndex]);// 获取特殊标记按钮 0 30 NSLog(@"%ld",[self.actionSheet firstOtherButtonIndex]); // 获取首个按钮的下标 1 31 } 32 33 // 弹出 actionSheet 34 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 35 [self.actionSheet showInView:self.view]; 36 37 /* 38 * 下面是几种弹出方式,会根据风格不同展现不同的方式 39 * - (void)showFromToolbar:(UIToolbar *)view 40 * - (void)showFromTabBar:(UITabBar *)view 41 * - (void)showInView:(UIView *)view 42 * - (void)showFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated 43 * - (void)showFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated 44 * - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated 45 */ 46 } 47 48 #pragma mark - <UIActionSheetDelegate> 49 // 即将显示时触发 50 - (void)willPresentActionSheet:(UIActionSheet *)actionSheet { 51 NSLog(@"%s",__FUNCTION__); 52 } 53 54 // 已经显示时触发 55 - (void)didPresentActionSheet:(UIActionSheet *)actionSheet { 56 NSLog(@"%s",__FUNCTION__); 57 } 58 59 // 点击某个按钮触发 60 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 61 NSLog(@"%s",__FUNCTION__); 62 } 63 64 // 即将消失时触发 65 - (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex { 66 NSLog(@"%s",__FUNCTION__); 67 } 68 69 // 已经消失时触发 70 - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex { 71 NSLog(@"%s",__FUNCTION__); 72 } 73 74 // 当用户按下 Home 键时触发 75 - (void)actionSheetCancel:(UIActionSheet *)actionSheet { 76 NSLog(@"%s",__FUNCTION__); 77 } 78 79 @end
运行效果
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律