如何使用UIAlertController替换UIActionSheet? [英] How to use UIAlertController to replace 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!