如何在 UIActionSheet 中为按钮设置 valueForKey?

How to set valueForKey in UIActionSheet for buttons?

我完全改造了一个旧项目,因为他们使用 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,因为他们试图添加按钮,但我收到这样的错误,

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key buttons.'

指导我克服这个:)

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

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

_buttons 没有出现在 public 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];
}