2

我正在使用iCarousel 库并且遇到了一些问题。

在控件演示示例项目中,使用了 XIB 文件,并且视图设置如下:

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    if (!view)
    {
        //load new item view instance from nib
        //control events are bound to view controller in nib file
        //note that it is only safe to use the reusingView if we return the same nib for each
        //item view, if different items have different contents, ignore the reusingView value
        view = [[[NSBundle mainBundle] loadNibNamed:@"ItemView" owner:self options:nil] lastObject];
    }
    return view;
}

因为我使用的是 Storyboard,所以我创建了一个视图控制器并像这样设置视图:

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

    NSString * storyboardName = @"MainStoryboard";
    NSString * viewControllerID = @"DuuinNewsItem";
    UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];

    //create new view if no view is available for recycling
    if (!view)
    {
        DUDuuin *tDuuin = [_duuins objectAtIndex:index];
        DUNewsItemViewController * controller = (DUNewsItemViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
        controller.duuin =  tDuuin;
        view = controller.view;
        [view setFrame:CGRectMake(0, 0, 314.0f, 415.0f)];

    }

    return view;
}

当我向视图中的任何按钮添加操作时,我收到错误消息:

在此处输入图像描述

我已经尝试了很多在 Stackoverflow 中推荐的东西,但我找不到解决方案:

我试过了:

  • 将出口设置为强(有人说这是一个问题,因为 ARC)
  • 在操作中删除发件人
  • 在具有 icarousel 的视图控制器中添加方法

****更新****

现在我看到了其他问题。当我在 DUNewsItemViewController 中定义 Action 并在模拟器中尝试时,它说:

-[__NSCFType btnTest:]: unrecognized selector sent to instance 0x1577c650

因此,我在具有 iCarousel 的 View Controller 的 .m 文件中添加了该方法,但问题仍然相同:

EXC_BAD_ACCESS (code=2)

一些信息:

  • 我正在使用 ARC
  • 它在故事板上
  • 观看次数是动态的
4

1 回答 1

3

您在这里的主要问题不是具体的视图,而是您的DUNewsItemViewController实例。您在提供的方法中创建控制器并返回视图。视图由 保留iCarouselDUNewsItemViewController另一方面,你不是。没有什么强烈指向它。由于没有指向它的强指针,它正在被释放。您的视图与所有按钮都正确显示,因为它再次保留在您的DUNewsItemViewController. 当按下按钮时,它会尝试访问其操作方法并失败,因为控制器不再存在。

要解决此问题,您需要创建一个指向控制器的强指针(而不是您之前尝试的按钮)。我不推荐一种完美的策略,而是一种有效的策略。

您可以创建一个可变数组(作为属性)并在创建视图控制器时将它们添加到其中(如果有多个)。这样,作为iCarousel的委托/数据源的控制器持有对它的引用:

if (!view)
{
    DUDuuin *tDuuin = [_duuins objectAtIndex:index];
    DUNewsItemViewController * controller = (DUNewsItemViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
    // Add controller to array to hold a strong reference to it.
    [self.myMutableArray addObject:controller];
    //
    controller.duuin =  tDuuin;
    view = controller.view;
    [view setFrame:CGRectMake(0, 0, 314.0f, 415.0f)];

}

否则,如果只有一个(或几个),您可以创建独特的网点:

@property (strong, nonatomic) DUNewsItemViewController *firstVC; 

并在您的carousel:viewForItemAtIndex:reusingView:view方法中包含一行self.firstVC = controller.

如果有什么不明白的,请告诉我。同样,可能有更好的实现,但这应该可行。XIB 在示例中起作用的原因是因为它们UIViews不是UIViewController您正在使用的子类。同样,您的视图正在正常工作(显示,就像示例一样),但是您的控制器正在被释放(不像示例,因为没有使用控制器)。

于 2013-06-05T23:37:43.383 回答