我有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的实例仍然存在=>

我究竟做错了什么?

最佳答案

您的SettingsViewController不会返回到创建它的InitViewController。相反,它将创建一个InitViewController实例,并将其呈现出来。因此,您最终得到了一堆视图控制器,它们在InitViewControllerSettingsViewController实例之间交替。

由于您在展示任何类型的视图控制器后都不会将其关闭,因此无法释放它们。

您的-[SettingsViewController back:]操作应自行关闭,而不是创建和呈现新的InitViewController

@implementation SettingsViewController

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

关于ios - 没有释放 View Controller ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13500702/

10-10 04:18