[英]View controllers not being deallocated
我有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
實例並將其呈現。 因此,您最終得到了一堆視圖控制器,它們在InitViewController
和SettingsViewController
實例之間交替。
由於您在展示任何類型的視圖控制器后都不會將其關閉,因此無法釋放它們。
您的-[SettingsViewController back:]
操作應自行關閉,而不是創建並展示一個新的InitViewController
。
@implementation SettingsViewController
- (IBAction)back:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.