【问题标题】:Error on opening popviewcontroller in iOS 7在 iOS 7 中打开 popviewcontroller 时出错
【发布时间】:2013-09-26 08:04:30
【问题描述】:

对我来说这看起来很简单,但我无法理解我犯了什么错误。我必须在我的 iPad 应用程序中单击行时打开一个弹出窗口。我已经完成了以下代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    popViewController *vc = [[popViewController alloc] initWithNibName:@"popViewController" bundle:nil];
    vc.preferredContentSize = CGSizeMake(500,500);
    vc.view.frame = CGRectMake(0, 0, 500, 500);

    UIPopoverController *healthPopOver = [[UIPopoverController alloc] initWithContentViewController:vc];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [healthPopOver presentPopoverFromRect:cell.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

应用程序在执行最后一行时崩溃。我在网上搜索了很多页面,但我无法找到它的原因。我没有收到任何特定错误,只有主文件中的线程 1 错误。

我使用的是 iOS 7。

【问题讨论】:

  • 问题在于没有保留弹出框。按照@null 的建议保留弹出框。

标签: iphone ios ipad uipopovercontroller


【解决方案1】:

尝试将您的 healthPopOverit 添加为您的类的成员,因为 UIPopoverControllers 必须保存在实例变量中。

在您的 .m 中将其定义为属性:

UIPopoverController *healthPopOver;

并改变:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    popViewController *vc = [[popViewController alloc] initWithNibName:@"popViewController" bundle:nil];
    vc.preferredContentSize = CGSizeMake(500,500);
    vc.view.frame = CGRectMake(0, 0, 500, 500);

    self.healthPopOver = [[UIPopoverController alloc] initWithContentViewController:vc];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [healthPopOver presentPopoverFromRect:cell.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

【讨论】:

  • 非常感谢它的工作......虽然这对我来说听起来很奇怪。你知道它这样做的原因吗?
  • @pankaj 我猜你必须使用 ARC。所以你的局部变量healthPopOver将在tableView: didSelectRowAtIndexPath:函数结束后被ARC释放。此时可能会出现现金。
  • UIPopoverController 没有超级视图,除非您不保留它,否则它不会被任何人保留,因此通过执行 UIPopoverController *healthPopOver = [[UIPopoverController alloc] init ... 变量超出范围并且对象由于不再拥有所有者而被释放,这就是您需要保留它的原因。
猜你喜欢
  • 2016-04-25
  • 1970-01-01
  • 2015-08-08
  • 1970-01-01
  • 2013-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-02
相关资源
最近更新 更多