2012-11-13 46 views
0

有人可以幫助我嗎?我試圖製作一個按鈕旋轉木馬 - 每個按鈕都有不同的背景圖像,但我沒有做到。iCarousel buttonsDemo'UIImage'可能不會響應'objectAtIndex'

最後,它的功能,但我不知道,爲什麼我總是警告(上 [圖像objectAtIndex:0],[圖像objectAtIndex:1],[圖像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

回答

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]; 
+0

非常感謝!你真的幫了我:) – Iva

+0

它現在的功能:)我試圖應用相同的代碼到故事板,但我突然在'@implementation iCarouselExampleViewController'上得到了一個新警告**不完整的實現**和**方法'carousel:viewForItemAtIndex: 'in protocol not implemented。** NSLog顯示:_由於未捕獲的異常'NSInvalidArgumentException'導致應用程序終止,原因:' - [iCarouselExampleViewController carousel:viewForItemAtIndex:]:無法識別的選擇器已發送到實例0x7551890'_你知道什麼是錯誤的? – Iva

+0

模擬器結束,XCode顯示SIGABRT。 – Iva

相關問題