Can someone help me, please? I am trying to make a carousel of button - every button has a different background image, but I failed to do it.
Finally it functions, but I don´t know, why I got always a warning (on [image objectAtIndex:0], [image objectAtIndex:1], [image objectAtIndex:2] ):
'UIImage' may not respond to 'objectAtIndex'.
Thank you very much!
My source code of iCarouselExampleViewController.m:
#import "iCarouselExampleViewController.h"
@implementation iCarouselExampleViewController
@synthesize carousel;
- (void)dealloc
{
carousel.delegate = nil;
carousel.dataSource = nil;
[carousel release];
[super dealloc];
}
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
//configure carousel
carousel.type = iCarouselTypeCoverFlow2;
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.carousel = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#pragma mark -
#pragma mark iCarousel methods
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
//generate 100 buttons
//normally we'd use a backing array
//as shown in the basic iOS example
//but for this example we haven't bothered
return 5;
}
- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
//limit the number of items views loaded concurrently (for performance reasons)
return 5;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton *button = (UIButton *)view;
if (button == nil)
{
UIImage *image=[NSArray arrayWithObjects:
[UIImage imageNamed:@"image1.png"],
[UIImage imageNamed:@"image2.png"],
[UIImage imageNamed:@"image3.png"],nil];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 200.0f, 200.0f);
[button setTitle:[NSString stringWithFormat:@"%i", index] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
switch (index) {
case 0:
{
[button setImage:(UIImage*)[image objectAtIndex:0] forState:UIControlStateNormal];
}
break;
case 1:
{
[button setImage:(UIImage*)[image objectAtIndex:1] forState:UIControlStateNormal];
break;
}
case 2:
{
[button setImage:(UIImage*)[image objectAtIndex:2] forState:UIControlStateNormal];
}
break;
}
}
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return button;
}
#pragma mark -
#pragma mark Button tap event
- (void)buttonTapped:(UIButton *)sender
{
//get item index for button
NSInteger index = [carousel indexOfItemViewOrSubview:sender];
[[[[UIAlertView alloc] initWithTitle:@"Button Tapped"
message:[NSString stringWithFormat:@"You tapped button number %i", index]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] autorelease] show];
}
@end
@end
because a UIImage isnt an NSArray but you only DECLARED it a UIImage hence no crash
UIImage *image=[NSArray arrayWithObjects:
[UIImage imageNamed:@"image1.png"],
[UIImage imageNamed:@"image2.png"],
[UIImage imageNamed:@"image3.png"],nil];
change it so an array is declared
NSArray *image=[NSArray arrayWithObjects:...
and while at it, rename image to images and use the new syntax
NSArray *images = @[img1,img2,img3];