我开始使用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.'
- 我添加了 QuartzCore 框架
- 将 iCarousel 复制到我的项目中
- 从示例中复制了 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