EGOTableViewPullRefresh 崩溃

发布于 2024-10-23 23:24:12 字数 697 浏览 5 评论 0原文

Xcode 4 告诉我它在这行代码处崩溃: view.delegate = self;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.title = @"Blog";

    if (_refreshHeaderView == nil) {

        EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.tableView.bounds.size.height, self.view.frame.size.width, self.tableView.bounds.size.height)];
        view.delegate = self;
        [self.tableView addSubview:view];
        _refreshHeaderView = view;
        [view release]; 
    }
    [_refreshHeaderView refreshLastUpdatedDate];
}

您知道它崩溃的原因吗?

Xcode 4 tells me that it crashes on this line of code: view.delegate = self;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.title = @"Blog";

    if (_refreshHeaderView == nil) {

        EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.tableView.bounds.size.height, self.view.frame.size.width, self.tableView.bounds.size.height)];
        view.delegate = self;
        [self.tableView addSubview:view];
        _refreshHeaderView = view;
        [view release]; 
    }
    [_refreshHeaderView refreshLastUpdatedDate];
}

Do you have any idea why it crashes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我一直都在从未离去 2024-10-30 23:24:12

您过度释放了视图。您初始化view,然后将其分配给_refreshHeaderView,然后立即释放view

当您执行此操作时:

_refreshHeaderView = view;

...您是在告诉 _refreshHeaderView 指向 view 对应的内存位置。 view 的保留计数为 1,因为您已经分配/初始化了它。然后在下一行中释放 view ,这意味着它的保留计数为 0,因此该对象不再存在(我在这里简化:您还将它添加为子视图,这会增加保留但重点是如果你不需要在这里释放它)。

这也意味着 _refreshHeaderView 也不再存在,因为视图和 _refreshHeadView 是同一个对象。因此,当您调用 refreshLastUpdatedDate 时,您将遇到访问错误并崩溃。

摆脱 [view release] 应该可以阻止崩溃,但是您应该非常小心,以确保稍后在使用完该对象后释放该对象(大概是在 dealloc 方法中)。建议将 refreshHeaderView 设置为一个属性来帮助解决此问题。

You're over-releasing the view. You init view, you then assign it to _refreshHeaderView, and then you immediately release view.

When you do this:

_refreshHeaderView = view;

...you're telling _refreshHeaderView to point to the memory location that view corresponds to. view has a retain count of 1, because you've alloc/init'd it. And then in the next line you release view, which means its retain count is 0, so the object no longer exists (I am simplifying here: you also add it as a subview, which would increment the retain count. But the point is if you don't need to release it here).

That also means _refreshHeaderView no longer exists either, because both view and _refreshHeadView are the same object. So when you call refreshLastUpdatedDate you'll get a bad access, and a crash.

Getting rid of the [view release] should stop the crash, but you should be very careful to make sure you release that object later on when you're done with it (presumably in your dealloc method). It would be advisable to make refreshHeaderView a property to help with this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文