iCarousel buttonsDemo'UIImage'可能无法响应'objectAtIndex'

时间:2012-11-13 20:23:04

标签: ios button warnings icarousel

有人可以帮帮我吗?我正在尝试制作按钮旋转木马 - 每个按钮都有不同的背景图像,但我没有做到。

最后它起作用了,但我不知道,为什么我总是警告(on     [image objectAtIndex:0],[image objectAtIndex:1],[image objectAtIndex:2] ):

'UIImage'可能无法响应'objectAtIndex'。

非常感谢!

我的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

1 个答案:

答案 0 :(得分:0)

因为UIImage不是NSArray 但你只是宣布它是一个UIImage因此没有崩溃

UIImage *image=[NSArray arrayWithObjects:
                          [UIImage imageNamed:@"image1.png"],
                          [UIImage imageNamed:@"image2.png"],
                          [UIImage imageNamed:@"image3.png"],nil];

更改它以便声明一个数组

NSArray *image=[NSArray arrayWithObjects:...

在此期间,将图像重命名为图像并使用新语法

NSArray *images = @[img1,img2,img3];