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

时间:2016-02-17 13:59:11

标签: ios objective-c uiactionsheet

我完全改造了旧项目之一,因为他们使用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:]:这个   class不是键值按键编码兼容的键。'

引导我过来这个:))

2 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:0)

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

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