【问题标题】:iCarousel Not Showing up and Method not being handlediCarousel 未显示且未处理方法
【发布时间】:2014-07-30 08:39:14
【问题描述】:

我正在尝试实现一个从右到左的 iCarousel 对象,两边都有淡入淡出。

所以我将 GtiHub 项目导入到我的应用程序中,将 UIView 分配给 iCarousel...将其链接到我的 MainViewController 上的 IBOutLet 并像 UITableViewController 一样传递 Delegate 和 DataSource。

虽然它不会出现,我不确定是什么导致了这个问题。

我的 iCarousel 数据源是一个 NSMutableArray *items;设计成这样:

for (int i = 0; i < 100; i++)
{
    [_items addObject:@(i)];
}

我在 ViewDidLoad 中初始化 iCarousel,如下所示

- (void)viewDidLoad
{
  [super viewDidLoad];
  //Initialize Carousel
  _carousel = [[iCarousel alloc] init];
  _carousel.delegate = self;
  _carousel.dataSource = self;

  //Carousel Testing Data
  for (int i = 0; i < 100; i++)
  {
     [_items addObject:@(i)];
  }

  //configure carousel
  _carousel.type = iCarouselTypeRotary;

}

我的轮播代理方法如下:

#pragma mark -
#pragma mark iCarousel methods

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
   //return the total number of items in the carousel
   return [_items count];
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
   UILabel *label = nil;

   //create new view if no view is available for recycling
   if (view == nil)
   {
      view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)];
      ((UIImageView *)view).image = [UIImage imageNamed:@"page.png"];
      view.contentMode = UIViewContentModeCenter;

      label = [[UILabel alloc] initWithFrame:view.bounds];
      label.backgroundColor = [UIColor clearColor];
      label.font = [label.font fontWithSize:50];
      label.tag = 1;
      [view addSubview:label];
   }
  else
   {
      //get a reference to the label in the recycled view
      label = (UILabel *)[view viewWithTag:1];
   }


  label.text = [_items[index] stringValue];
  return view;
}

 - (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
 {
    switch (option)
    {
       case iCarouselOptionFadeMin:
           return -0.2;
       case iCarouselOptionFadeMax:
           return 0.2;
       case iCarouselOptionFadeRange:
           return 2.0;
       default:
           return value;
     }
  }

情节提要中的所有内容似乎都已连接,数据应自行呈现,出了什么问题,我该如何解决这个问题?

【问题讨论】:

  • 你能接受让你工作的答案吗?

标签: ios objective-c uiview delegates icarousel


【解决方案1】:

由于您在视图加载后设置了 DataSource 数组 (_items):

  1. 确保故事板中的轮播视图确实具有连接到控制器的 IBOutlet,但链接到文件所有者(控制器或视图)的“委托”和“数据源” .

  2. 仅将 _carousel.delegate 和 _carousel.dataSource 设置为“self”初始化和更新 dataSource 数组(本例中为 _items)之后。

【讨论】:

    【解决方案2】:

    放置断点以检查 iCarousel Datasource 和 Delegate 方法是否被调用。 如果没有调用方法,请检查控制器是否遵循 iCarousel 协议,如下所示:

        @interface yourViewController : UIViewController <iCarouselDataSource, iCarouselDelegate>
    

    显然你必须这样做

       #import "iCarousel.h"
    

    【讨论】:

    • 这两个设置以及委托和数据源,我已经应用了断点并且它们出现了
    【解决方案3】:

    更新:在您的 viewDidLoad 中,您有

      _carousel = [[iCarousel alloc] init];
    

    您好像忘记为 _carousel 设置框架并将其添加为子视图。

    【讨论】:

    • 我在 Storyboard 上有一个 UIView 并通过 IBOutlet 连接它,那还需要添加为子视图吗?
    • 如果你在Controller场景中已经有了IBOutlet,就不需要_carousel = [[iCarousel alloc] init];这将创建一个 iCarousel 的新实例,并且您的 _carousel 将指向新实例,该实例不会作为子视图添加,也没有框架。
    • 好的,所以我把初始化从那里拿出来了,它仍然没有出现
    【解决方案4】:

    要获取 imageView 参考,试试这个:

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)];
    imageView.image = [UIImage imageNamed:@"page.png"];
    view = imageView;
    

    【讨论】:

    • 而不是这一行? view.contentMode = UIViewContentModeCenter;
    • 我试过了,加载时我得到了相同的视图,在另一个笔记上我发现 NSArray *Items 返回 0 个项目
    • 该死的!然后只需添加 _items = [NSMutableArray 数组];就在“for”循环之前。
    • 由于某种原因它仍然返回 o
    • 如果我们可以将此移至聊天@instaable,我可以与您分享我更新的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 2011-12-18
    • 1970-01-01
    • 2013-09-08
    相关资源
    最近更新 更多