ios - 从 UITableViewController 创建时何时释放 UINavigationController?

标签 ios

我的情况很不寻常,我不太了解内存管理。

我有一个 UITableViewController 用于显示消息,然后创建一个 UINavigationController 并将其 View 添加为当前 View 的 subview 以显示它。我遇到的问题是 Xcode 报告我有一个潜在的内存泄漏(我同意)由于没有释放 UINavigationController,但是当我在下面的代码中释放它时,当我单击返回返回到 TableView 。

我在 UITableViewController 中使用了一个保留的属性来跟踪当前的 UINavigationController 并管理保留计数,但显然我在这里遗漏了一些东西。

注意:当单击后退按钮并显示消息 -[UILayoutContainerView removeFromSuperview:]: unrecognized selector sent to instance 0x5537db0 时发生崩溃

另请注意,如果我删除 [nc release] 代码行,它就可以正常工作。

这是我的代码:

@property(nonatomic, retain) UINavigationController *currentNavigationController;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UINavigationController *nc = [[UINavigationController alloc] init];

    CGRect ncFrame = CGRectMake(0.0, 0.0, [[self view] frame].size.width, [[self view] frame].size.height);
    [[nc view] setFrame:ncFrame];

    // I created a CurrentNavigationController property to 
    // manage the retain counts for me
    [self setCurrentNavigationController:nc]; 

    [[self view] addSubview:[nc view]];
    [nc pushViewController:messageDetailViewController animated:YES];


    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonSystemItemRewind target:[nc view] action:@selector(removeFromSuperview:)];


    nc.navigationBar.topItem.leftBarButtonItem = bbi;
    [bbi release];

    [nc release];
}

最佳答案

您创建的 UINavigationController“nc”只能在此方法内使用。在这个方法之后它不会存储在任何地方(因为你释放了它)。因此,您将 navigationController 的 View 作为 subview 添加到您的类 View 中,然后删除 navigationController。那是错误的。当 View 和 viewController 尝试引用它们的 navigationController 时(当它不存在时),您的应用程序将崩溃。

这里是 didSelectRowForIndexPath 方法的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{

    UINavigationController *nc = [[UINavigationController alloc] init];

    CGRect ncFrame = CGRectMake(0.0, 0.0, [[self view] frame].size.width, [[self view] frame].size.height);
    [[nc view] setFrame:ncFrame];

    [self setCurrentNavigationController:nc]; 
    [nc release];

    [[self view] addSubview:[self.currentNavigationController view]];

    UIViewController *viewCont = [[UIViewController alloc] init];
    [viewCont.view setBackgroundColor:[UIColor greenColor]];

    [nc pushViewController:viewCont animated:YES];

    NSLog(@"CLASS %@",[[self.currentNavigationController view]class]);

    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonSystemItemRewind target:[self.currentNavigationController view] action:@selector(removeFromSuperview)];


    self.currentNavigationController.navigationBar.topItem.leftBarButtonItem = bbi;
    [bbi release];
}

removeFromSuperview 方法的选择器最后不应该有“:”。它没有参数 :)

关于ios - 从 UITableViewController 创建时何时释放 UINavigationController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12400558/

相关文章:

ios - MKMapView 中的所有 Annotation 对用户不可见

iOS 13 公测版 2 - Combine.Future "dyld: Symbol not found"

ios - 以编程方式设置为 Root View Controller 时, View Controller 无法正确显示 subview

ios - Xcode 10/Swift 中的 Vision API 没有检测到任何东西。我做错了什么吗?

ios - 如何用音频图表播放声音效果

IOS:触摸外层蒙版但在框架内

ios - "Cannot convert value of type '(字符串)-> bool 值 ' to expected argument "

ios - 使用 NIB 重用自定义单元

ios - 设计循环 "selector"控件

ios - 在另一个不相关的 View Controller 中引用属性