当应用程序进入后台时,我的 View Controller 类被释放。我正在使用ARC。
我有一个 UIViewController,它在应用程序变为活动状态并执行方法时订阅通知。但是一旦应用程序在后台运行大约 30 秒然后恢复,应用程序就会崩溃,并显示“发送到已释放实例的消息”。
启用 Zombie 对象表明 View Controller 本身就是 Zombie。
谢谢!
我的视图控制器的实例化(在 AppDelegate 中):
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
MyViewController *myViewController = [storyBoard instantiateViewControllerWithIdentifier:@"MyViewController"];
AppDelegate 中的前台通知:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationForegrounded object:self];
}
视图控制器中的前台通知:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resumeForeground) name:kNotificationForegrounded object:nil];
}
我尝试在 AppDelegate 中创建一个强引用,但视图控制器仍然被释放:
@property (strong, nonatomic) MyViewController *myViewController;
我尝试将视图控制器添加到数组并在 AppDelegae 中对数组进行强引用,但仍然得到相同的结果:
@property (strong, nonatomic) NSMutableArray *viewControllers;
//...
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
MyViewController *myViewController = [storyBoard instantiateViewControllerWithIdentifier:@"MyViewController"];
self.viewControllers = [[NSMutableArray alloc] init];
[self.viewControllers addObject:myViewController];