I'm making an iPhone app which consists of carousel. Please can anyone tell how we perform an action on selected image of Carousel.

- (void)carousel:(iCarousel *)crsl didSelectItemAtIndex:(NSInteger)index { }

I'm trying to implement this but its not giving correct result please can anyone tell correct implementation of this

thanks

有帮助吗?

解决方案

Set the DataSource and Delegate of iCarousel like this

enter image description here

And than in .h file set your ViewController Delegate for your iCarousel

#import <UIKit/UIKit.h>
#import "iCarousel.h"

@interface iCarouselViewController : UIViewController<iCarouselDataSource, iCarouselDelegate>

@property (strong, nonatomic) NSMutableArray *images;
@property (nonatomic, retain) IBOutlet iCarousel *carousel;

@end

And in .m file write the delegate method didSelectItemAtIndex like this

    -(void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{

    UIImage * img = [images objectAtIndex:index];

    ImageViewController *aImageViewController = [[ImageViewController alloc] initWithNibName:@"ImageViewController" bundle:nil];

    aImageViewController.image = img;   //this code is used to send image to another view not tested

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:aImageViewController];
    navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;

//this code used for present second-view controller that display selected image
    navigationController.topViewController.title = @"Greeting's";
    [self presentModalViewController:navigationController               animated:YES];
    [navigationController release];

}

其他提示

once try like this,

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{

        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[menuViews objectAtIndex:index]]];
        _reflectionView =[[[ReflectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, image.size.width, image.size.height)] autorelease];
        UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)] autorelease];
        [button setBackgroundImage:image forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleLabel.font = [button.titleLabel.font fontWithSize:14];
        [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = index;
        [_reflectionView addSubview:button];

    return _reflectionView;
}

in above code menuViews is images array.

- (void)buttonTapped:(UIButton *)sender
{   
    NSLog(@"%d",sender.tag);//based on tag value you can do whatever you want
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top