查看控制器未被释放

时间:2012-11-21 19:31:20

标签: ios view

我有2个控制器InitViewController和SettingsViewController。每个视图都有一个调用另一个视图的按钮:

InitViewController

@interface InitViewController : UIViewController
    - (IBAction)loadSettings:(id)sender;
@end

@implementation InitViewController
    - (IBAction)loadSettings:(id)sender {
        SettingsViewController *vc = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
        [self presentViewController:vc animated:YES completion:nil];
        vc = nil;
    }
@end

SettingsViewController

@interface SettingsViewController : UIViewController
    - (IBAction)back:(id)sender;
@end

@implementation SettingsViewController
    - (IBAction)back:(id)sender {
        InitViewController *vc = [[InitViewController alloc] initWithNibName:@"InitViewController" bundle:nil];
        [self presentViewController:vc animated:YES completion:nil];
        vc = nil;
    }
@end

当我分析应用程序并通过多次点击按钮测试它时,我看到InitViewController和SettingsViewController的实例仍然存在=> PrintScreen

我做错了什么?

1 个答案:

答案 0 :(得分:5)

您的SettingsViewController未返回创建它的InitViewController。相反,它会创建一个 InitViewController实例,并提供该实例。因此,您最终会在InitViewControllerSettingsViewController的实例之间交替使用一堆视图控制器。

由于您在呈现视频控制器后从不关闭任何类型的视图控制器,因此无法解除分配。

您的-[SettingsViewController back:]行动应该解雇,而不是创建和展示新的InitViewController

@implementation SettingsViewController

- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}