ios - iCarousel 数据源问题

标签 ios icarousel

我开始使用 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

最佳答案

我不知道到底是什么问题,但看了其他几篇文章后,我认为这是前 5 个问题,希望对您有所帮助,祝您好运

1.

I've found the most common place this error happens is when you instantiate a view from a xib from within a class that is not the xib's owner.

What I mean by this is you might be calling something similar to this:

[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil]; You're changing the owner, so you have to be sure that the class that self refers to has all the IBOutlet properties needed by "MyView". Usually this is done in Interface Builder, but in this case you're setting your owner programmatically, which means you can't make the connections in IB. When the IBOutlets aren't there, the app tries to make those connections and fails, giving the error you see.

My advice (without knowing any more info than you've given so far) is to check to see if you've made this call without having the proper IBOutlets.

2.

I had connected an UIControl outlet in the interface builder to the IBOutlet in the xib's owner. For some reason, the IBOutlet was deleted from the owner, but the reference to the outlet remained dangling in the xib. This would always give me the error Lesson learnt: When deleting any outlets for vars in the implementation, make sure to unhook the respective connection in the IB

3.

I found the problem, it was because of a button i used in the AboutViewController, i didnt declare the property for that button.

4.

Also when you rename a view, don't forget to delete the reference on File's Owner. It may also raise this error.

5.

Fixed - went to iOS Simulator > Reset Content and Setting

关于ios - iCarousel 数据源问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18253190/

相关文章:

ios - 有人可以帮助我了解 XCode 的工作原理吗?

ios - 自动布局为顶部底部和前导提供 8 个间距

ios - 如何在 iOS 6 上的 Safari 中获取此栏?

iphone - 如何使用此代码在 iCarousel 中加载多个图像?

ios - iCarousel 线性类型从中心开始

iphone - Instagram iPhone 应用程序问题

ios - 在 iphone 上删除 Beta 测试 (testflight) 应用程序

ios - 加载 ICarousel 的 MPMovieControl 无法正常工作

ios - 在 Swift 中将可视格式语言与 iCarousel 结合使用

ios - 如何为 iCarousel 提供可访问的滑动操作