【问题标题】:How to set valueForKey in UIActionSheet for buttons?如何在 UIActionSheet 中为按钮设置 valueForKey?
【发布时间】:2016-02-17 13:59:11
【问题描述】:

我正在彻底改造一个旧项目,因为他们使用UIActionSheet,我不熟悉它,所以请帮我找出来。

UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@"beard Selection" delegate:self cancelButtonTitle:@"Cancel"   destructiveButtonTitle:@""   otherButtonTitles:@"", @"", @"", @"", @"", @"", @"", @"", nil];

    NSLog(@"kishore calculation ");
    [[[popupQuery valueForKey:@"_buttons"] objectAtIndex:0] setImage:[UIImage imageNamed:@"ca1.png"] forState:UIControlStateNormal];
    [[[popupQuery valueForKey:@"_buttons"] objectAtIndex:1] setImage:[UIImage imageNamed:@"ca2.png"] forState:UIControlStateNormal];
    [[[popupQuery valueForKey:@"_buttons"] objectAtIndex:2] setImage:[UIImage imageNamed:@"ca3.png"] forState:UIControlStateNormal];
    [[[popupQuery valueForKey:@"_buttons"] objectAtIndex:3] setImage:[UIImage imageNamed:@"ca4.png"] forState:UIControlStateNormal];
    [[[popupQuery valueForKey:@"_buttons"] objectAtIndex:4] setImage:[UIImage imageNamed:@"ca5.png"] forState:UIControlStateNormal];
    [[[popupQuery valueForKey:@"_buttons"] objectAtIndex:5] setImage:[UIImage imageNamed:@"ca6.png"] forState:UIControlStateNormal];
    [[[popupQuery valueForKey:@"_buttons"] objectAtIndex:6] setImage:[UIImage imageNamed:@"ca7.png"] forState:UIControlStateNormal];
    [[[popupQuery valueForKey:@"_buttons"] objectAtIndex:7] setImage:[UIImage imageNamed:@"ca8.png"] forState:UIControlStateNormal];
    [[[popupQuery valueForKey:@"_buttons"] objectAtIndex:8] setImage:[UIImage imageNamed:@"ca9.png"] forState:UIControlStateNormal];
    [[[popupQuery valueForKey:@"_buttons"] objectAtIndex:9] setImage:[UIImage imageNamed:@"ca10.png"] forState:UIControlStateNormal];

    popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [popupQuery setTag:Cap];
    [popupQuery showInView:self.view];

UIActionSheet 就像UIAlertView,因为他们试图添加按钮,但我收到这样的错误,

由于未捕获的异常“NSUnknownKeyException”而终止应用程序, 原因:'[valueForUndefinedKey:]:这个 类不符合键按钮的键值编码。'

指导我克服这个:)

【问题讨论】:

  • UIActionSheet 已被弃用,为什么你不尝试 uialertcontroller

标签: ios objective-c uiactionsheet


【解决方案1】:

_buttons 没有出现在公共 Api 中。如果你使用这个苹果不同意你的应用程序。所以根据新概念实现你的代码,像这样

UIAlertController * view=   [UIAlertController
                             alertControllerWithTitle:@"XXX "
                             message:@"pickAnyone"
                             preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* first = [UIAlertAction
                         actionWithTitle:@"abc"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             //Do some thing here
                             [view dismissViewControllerAnimated:YES completion:nil];
                         }];
UIAlertAction* second = [UIAlertAction
                          actionWithTitle:@"cde+"
                          style:UIAlertActionStyleDefault
                          handler:^(UIAlertAction * action)
                          {
                              [view dismissViewControllerAnimated:YES completion:nil];
                          }];
   UIAlertAction* third = [UIAlertAction
                       actionWithTitle:@"hhh"
                       style:UIAlertActionStyleDestructive
                       handler:^(UIAlertAction * action)
                       {
                           [view dismissViewControllerAnimated:YES completion:nil];

                       }];
UIAlertAction* cancel = [UIAlertAction
                     actionWithTitle:@"Cancel"
                     style:UIAlertActionStyleDefault
                     handler:^(UIAlertAction * action)
                     {
                     }];

[first setValue:[[UIImage imageNamed:@"abc.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
[second setValue:[[UIImage imageNamed:@"cde+.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
[third setValue:[[UIImage imageNamed:@"hhh"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];

[view addAction:first];
[view addAction:second];
[view addAction:third];     
[view addAction:cancel];

[self presentViewController:view animated:YES completion:nil];
}

【讨论】:

  • 这就像你的代码和解释的警报控制器兄弟,现在我正在使用 tableview,如果它不好,那么我会使用你的答案。
  • 在这里我使用了UIAlertControllerStyleActionSheet,请在ans bro中查看一次
  • 是的,我理解兄弟,但对我来说有 20 张图片,所以我无法输入那么多,所以我更喜欢 tableview。
【解决方案2】:

此代码使用 KVO 尝试访问 UIActionSheet 类的私有属性。这是个坏主意,可能会导致您的应用被应用商店拒绝。

这是个坏主意,因为它依赖于 Apple 框架的私有实现细节。在这种情况下,它可能停止工作,因为 Apple 更改了这些私有实现细节。

【讨论】:

  • 好的兄弟谢谢,因为我是新手,它是旧代码所以我很困惑,现在我正在使用除 uiactionseet 之外的其他方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多