1

我开始使用iCarousel但收到此错误:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIView 0x8372530> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key dataSource.'
  1. 我添加了 QuartzCore 框架
  2. 将 iCarousel 复制到我的项目中
  3. 从示例中复制了 XIB

我已经使用了这个示例iCarousel

我的代码:

#import <UIKit/UIKit.h>
#import "iCarousel.h"

@interface UsersViewController : UIViewController <iCarouselDataSource, iCarouselDelegate>

@property (strong, nonatomic) IBOutlet iCarousel *aCarousel;
@property (strong, nonatomic) IBOutlet UILabel *label;
@property (nonatomic) BOOL wrap;

@property (strong, nonatomic) NSMutableArray *animals;
@property (strong, nonatomic) NSMutableArray *descriptions;

- (IBAction)onTapBackButton:(id)sender;

@end




#import "UsersViewController.h"

@interface UsersViewController ()

@end

@implementation UsersViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _wrap = NO;

        self.animals = [NSMutableArray arrayWithObjects:@"Bear.png",
                        @"Zebra.png",
                        @"Tiger.png",
                        @"Goat.png",
                        @"Birds.png",
                        @"Giraffe.png",
                        @"Chimp.png",
                        nil];

        self.descriptions = [NSMutableArray arrayWithObjects:@"Bears Eat: Honey",
                             @"Zebras Eat: Grass",
                             @"Tigers Eat: Meat",
                             @"Goats Eat: Weeds",
                             @"Birds Eat: Seeds",
                             @"Giraffes Eat: Trees",
                             @"Chimps Eat: Bananas",
                             nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.aCarousel.type = iCarouselTypeCoverFlow2;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Main Actions

- (IBAction)onTapBackButton:(id)sender
{
    [self dissmis];
}


#pragma mark - Main methods

- (void)dissmis
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma - mark iCarousel Delegate

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return [self.animals count];
}

- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
    // limit the number of item views loaded concurrently (for performance)
    return 7;
}

- (UIView*)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    // create a numbered view
    view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[self.animals objectAtIndex:index]]];
    return view;
}

- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
    return 0;
}

- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
    // usually this should be slightly wider than the item views
    return 240;
}

- (BOOL)carouselShouldWrap:(iCarousel *)carousel
{
    return self.wrap;
}

- (void)carouselDidScroll:(iCarousel *)carousel
{
    [self.label setText:[NSString stringWithFormat:@"%@", [self.descriptions objectAtIndex:carousel.currentItemIndex]]];
}

@end
4

1 回答 1

1

我不知道到底是什么问题,但是在查看了其他几个帖子之后,我认为前 5 个可能是问题所在,希望对您有所帮助,祝您好运 broski

1.

我发现这个错误发生的最常见的地方是当你从一个不是 xib 所有者的类中实例化一个 xib 的视图时。

我的意思是你可能会调用类似这样的东西:

[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil]; 您正在更改所有者,因此您必须确保 self 引用的类具有“MyView”所需的所有 IBOutlet 属性。通常这是在 Interface Builder 中完成的,但在这种情况下,您正在以编程方式设置您的所有者,这意味着您无法在 IB 中建立连接。当 IBOutlets 不存在时,应用程序会尝试建立这些连接并失败,并给出您看到的错误。

我的建议(不知道比你目前提供的更多信息)是检查你是否在没有适当的 IBOutlets 的情况下拨打了这个电话。

2.

我已将界面构建器中的 UIControl 插座连接到 xib 所有者的 IBOutlet。出于某种原因,IBOutlet 已从所有者中删除,但对插座的引用仍然悬在 xib 中。这总是会给我错误教训:在实现中删除任何 vars 的出口时,请确保在 IB 中解开相应的连接

3.

我发现了问题,这是因为我在 AboutViewController 中使用了一个按钮,我没有声明该按钮的属性。

4.

此外,当您重命名视图时,不要忘记删除文件所有者上的引用。它也可能引发此错误。

5.

已修复 - 转到 iOS 模拟器 > 重置内容和设置

于 2013-08-15T13:25:05.637 回答