如何使用UIAlertController替换UIActionSheet?

编程入门 行业动态 更新时间:2024-10-27 21:21:35
本文介绍了如何使用UIAlertController替换UIActionSheet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在维护一个基于SDK 6.0的旧iOS项目。

I am maintaining an old iOS project which based on SDK 6.0.

此项目的方法名为

- (void)showComboBox:(UIView *)view:withOptions:(NSDictionary *)options

is用于显示组合框。为了实现这一目标,它使用了UIActionSheet,这在iOS8上已弃用。

is used to show a combo box. To achieve the goal, it used UIActionSheet, which is deprecated on iOS8.

我的解决方案是这样的:

My solution is like this:

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber10_8) { UIAlertController* alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction* item = [UIAlertAction actionWithTitle:@"item" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { //do something here //inform the selection to the WebView ... [alertController dismissViewControllerAnimated:YES completion:nil]; }]; UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [alertController dismissViewControllerAnimated:YES completion:nil]; }]; [alertController addAction:item]; [alertController addAction:cancelAction]; //I am not sure whether it's the right way if ([view.nextResponder isKindOfClass:UIViewController.class]) { UIViewController* vc = (UIViewController*)view.nextResponder; [vc presentViewController:alertController animated:YES completion:nil]; }

这是一个合适的解决方案吗?

Is that a proper solution?

这是我主要关注:UIAlertController需要添加到UIViewController但是我只能得到UIView的指针,所以我用view.nextResponder得到我想要的东西,但这是一个好方法吗?

This is what I mostly concern about: UIAlertController needs to be added to a UIViewController but I can only get the pointer of the UIView, so I used view.nextResponder to get what I want, but it's that a good way?

推荐答案

我使用以下代码使用 UIAlertViewController 显示操作表它是完美的。

I have used following code to show action sheet using UIAlertViewController and it works perfect.

- (IBAction)buttonClicked:(id)sender { UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"Action Sheet" message:@"Using the alert controller" preferredStyle:UIAlertControllerStyleActionSheet]; [actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { // Cancel button tappped. [self dismissViewControllerAnimated:YES completion:^{ }]; }]]; [actionSheet addAction:[UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { // Distructive button tapped. [self dismissViewControllerAnimated:YES completion:^{ }]; }]]; [actionSheet addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { // OK button tapped. [self dismissViewControllerAnimated:YES completion:^{ }]; }]]; // Present action sheet. [self presentViewController:actionSheet animated:YES completion:nil]; }

修改:

你需要在这里获得 UIViewController 对象。您可以设置全局变量或调用委托方法,也可以使用通知来获取此代码中的视图控制器对象。

You need to get UIViewController object here. You can set global variable or call a delegate method, or you can use notification to get view controller object in this code.

并且上面代码中的最后一行将是。

and last line in above code will be like.

[self.viewController presentViewController:actionSheet animated:YES completion:nil];

self.viewController 是一个全局变量将在您实际获得此视图之前设置。

self.viewController is a global variable which will be set before you actually get this view.

因为您现在使用的方法是 view.nextResponder 。我担心它可能不起作用。

Because the approach you are following now using view.nextResponder. I'm afraid that it may not work.

更多推荐

如何使用UIAlertController替换UIActionSheet?

本文发布于:2023-11-27 19:31:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639315.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   UIAlertController   UIActionSheet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!