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
复制代码

运行效果

 

posted on   低头捡石頭  阅读(73)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示