gpt4 book ai didi

iphone - EXC_BAD_ACCESS (code=2 or code 1) 调用按钮 Action 时

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:09:53 24 4
gpt4 key购买 nike

我正在使用 iCarousel Library我遇到了一些问题。

在控件演示示例项目中,使用了一个 XIB 文件, View 设置如下:

- (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,所以我创建了一个 View Controller 并像这样设置 View :

- (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;
}

当我向 View 中的任何按钮添加操作时出现错误:

enter image description here

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

我试过:

  • 将 socket 设置为强(有人说这是一个问题,因为 ARC)
  • 删除操作中的发件人
  • 在具有 icarousel 的 View Controller 中添加方法

****更新****

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

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

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

EXC_BAD_ACCESS (code=2)

一些信息:

  • 我正在使用 ARC
  • 它在 Storyboard上
  • 观看次数是动态的

最佳答案

您的主要问题不在于具体的 View ,而在于您的 DUNewsItemViewController 实例。您使用提供的方法创建 Controller 并返回 View 。该 View 由 iCarousel 保留。另一方面,您的 DUNewsItemViewController 不是。没有任何东西强烈指向它。由于没有指向它的强指针,它正在被释放。您的 View 显示正确,包含所有按钮,因为它再次保留在 DUNewsItemViewController 之外。当按下按钮时,它会尝试访问其操作方法但会失败,因为 Controller 不再存在。

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

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

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 子类。同样,您的 View 正常工作(显示,就像示例一样),但是您的 Controller 正在被释放(不像示例,因为没有使用 Controller )。

关于iphone - EXC_BAD_ACCESS (code=2 or code 1) 调用按钮 Action 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16951144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com