0

有人能帮助我吗?我正在尝试制作一个按钮轮播 - 每个按钮都有不同的背景图像,但我没有做到。

最后它起作用了,但我不知道,为什么我总是收到警告(在 [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

@结尾

4

1 回答 1

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];
于 2012-11-13T20:28:44.570 回答