草庐IT

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

coder 2024-01-17 原文

我正在使用 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 中的任何按钮添加操作时出现错误:

我已经尝试了很多 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/

有关iphone - EXC_BAD_ACCESS (code=2 or code 1) 调用按钮 Action 时的更多相关文章

  1. ruby-on-rails - rails : How to make a form post to another controller action - 2

    我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak

  2. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  3. ruby-on-rails - 如何在 Rails Controller Action 上触发 Facebook 像素 - 2

    我有一个ruby​​onrails应用程序。我按照facebook的说明添加了一个像素。但是,要跟踪转化,Facebook要求您将页面置于达到预期结果时出现的转化中。即,如果我想显示客户已注册,我会将您注册后转到的页面作为成功对象进行跟踪。我的问题是,当客户注册时,在我的应用程序中没有登陆页面。该应用程序将用户带回主页。它在主页上显示了一条消息,所以我想看看是否有一种方法可以跟踪来自Controller操作而不是实际页面的转化。我需要计数的Action没有页面,它们是ControllerAction。是否有任何人都知道的关于如何执行此操作的gem、文档或最佳实践?这是进入布局文件的像素

  4. ruby-on-rails - 优雅的 Rails : multiple routes, 相同的 Controller Action - 2

    让多条路线去同一条路的最优雅的方式是什么ControllerAction?我有:get'dashboard',to:'dashboard#index'get'dashboard/pending',to:'dashboard#index'get'dashboard/live',to:'dashboard#index'get'dashboard/sold',to:'dashboard#index'这很丑陋。有什么“更优雅”的建议吗?一个类轮的奖励积分。 最佳答案 为什么不只有一个路由和一个Controller操作,并根据传递给它的参数来

  5. ruby-on-rails - Rails 2.3.5 : How does one access code inside of lib/directory/file. rb? - 2

    我创建了一个文件,这样我就可以在lib/foo/bar_woo.rb中的许多模型之间共享一个方法。在bar_woo.rb中,我定义了以下内容:moduleBarWoodefhelloputs"hello"endend然后在我的模型中我正在做类似的事情:defMyModel解释器提示它期望bar_woo.rb定义Foo::BarWoo。《使用Rails进行敏捷Web开发》一书指出,如果文件包含类或模块,并且文件使用类或模块名称的小写形式命名,那么Rails将自动加载文件。因此我不需要它。定义代码的正确方法是什么,在我的模型中调用代码的正确方法是什么? 最佳答案

  6. ruby 单元测试 : run some code after each failed test - 2

    在Test::Unit中的ruby​​单元测试断言失败后,在执行teardown之前,是否有一些简洁优雅的方法来立即执行我的代码?我正在做一些自动化的GUI测试,并希望在出现问题后立即截图。 最佳答案 如果您使用的是1.9,请不要使用Test::Unit::TestCase作为您的基类。对其进行子类化并覆盖#run_test以进行救援,截取屏幕截图并重新提出:classMyAbstractTestCase或者,我认为这实际上是最简洁的方法,您可以使用before_teardownHook:classMyTestCase这不适用于1.

  7. ruby-on-rails - Rails Rspec 测试 Controller 新 Action - 2

    我正在尝试在我的Controller中测试新操作。目前它看起来像这样:Controllerdefnew@business=Business.new@business.addresses.buildend规范describe'GET#new'doit'assignsanewbusinessto@business'doget:newexpect(assigns(:business)).tobe_a_new(Business)endend我想测试“@business.addresses.build”这一行。我该怎么做?提前致谢! 最佳答案

  8. ruby-on-rails - ApplicationController :Class 的未定义方法 `caches_action' - 2

    我正在尝试升级到Rails4beta1,但遇到了一些问题。简而言之,这就是我的应用程序Controller的样子。classApplicationControllercaches_action在Rails4中移到了它自己的gem中,因此包含gem应该可以解决问题。gem"actionpack-action_caching",github:"rails/actionpack-action_caching"但是当我运行我的请求规范或在浏览器中访问该应用程序时,我收到此错误。app/controllers/application_controller.rb:3:in`':undefinedm

  9. ruby - 如何将字符串格式的毫秒数转换为 HH :MM:SS format in Ruby in under 3 lines of code? - 2

    @scores_raw.eachdo|score_raw|#belowiscodeiftimewasbeingsentinmillisecondshh=((score_raw.score.to_i)/100)/3600mm=(hh-hh.to_i)*60ss=(mm-mm.to_i)*60crumbs=[hh,mm,ss]sum=crumbs.first.to_i*3600+crumbs[1].to_i*60+crumbs.last.to_i@scoressum,:hms=>hh.round.to_s+":"+mm.round.to_s+":"+ss.round.to_s}@score

  10. ruby-on-rails - Heroku Action 缓存似乎不起作用 - 2

    我一直在Heroku上尝试不同的缓存策略,并添加了他们的memcached附加组件,目的是为我的应用程序添加Action缓存。但是,当我在我当前的应用程序上查看Rails.cache.stats时(安装了memcached并使用dalligem),在执行应该缓存的操作后,我得到current和total_items为0。在Controller的顶部,我想缓存我有的Action:caches_action:show此外,我修改了我的环境配置(对于在Heroku上运行的配置)config.cache_store=:dalli_store我是否可以查看其他一些统计数据,看看它是否有效或我做错

随机推荐