1

我将 iCarousel 添加到我的应用程序中,但它不起作用。在我的另一个应用程序中它可以工作。我在 InterfaceBuilder 中链接了视图、委托和数据源

视图控制器.h

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


@interface ViewController : UIViewController <iCarouselDataSource, iCarouselDelegate>{

IBOutlet UIWebView *WebView;
NSURLRequest *Request;

...
}
...

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


@end

视图控制器.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize carousel;


- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
carousel.type = iCarouselTypeRotary;
carousel.center = CGPointMake(160.0, 250.0);
[self.view addSubview:carousel];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

 - (void)viewDidUnload
{
[super viewDidUnload];
 self.carousel = nil;

}

#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 100;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton *button = (UIButton *)view;
if (button == nil)
{
    //no button available to recycle, so create new one
    UIImage *image = [UIImage imageNamed:@"page.png"];
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setBackgroundImage:image forState:UIControlStateNormal];
    button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
    [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}

//set button label
[button setTitle:[NSString stringWithFormat:@"%i", index] forState:UIControlStateNormal];

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

2 回答 2

0

您是否尝试在数据源方法中放置断点以查看它们是否被调用?

如何使用调试器检查轮播的 dataSource 属性是否已在您的 iCarousel 视图中正确设置?

我的猜测是 dataSource 没有被正确绑定,但是如果没有更多信息就很难确定。

于 2012-10-23T20:26:42.150 回答
0

请检查NSArray以下方法的第一个数组不是 nil 写入它并在编译和构建后在数据源和委托方法上放置断点之后......它的工作代码......

- (void)awakeFromNib
{
    //set up data
    //your carousel should always be driven by an array of
    //data of some kind - don't store data in your item views
    //or the recycling mechanism will destroy your data once
    //your item views move off-screen
    self.items = [NSMutableArray array];
    for (int i = 0; i < 1000; i++)
    {
        [self.items addObject:@(i)];
    }
}

试试看....
谢谢

于 2016-05-06T11:26:52.970 回答