My View Controller class gets deallocated when the app goes to the background. I'm using ARC.
I have a UIViewController that subscribes to a notifications when the app becomes active and executes a method. But once the app is about 30 secs in the background and then resumes, the app crashes with "message sent to deallocated instance".
Enabling Zombie objects shows that the View Controller itself is the Zombie.
Thank you!
Instantiation of my view controller (in AppDelegate):
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
MyViewController *myViewController = [storyBoard instantiateViewControllerWithIdentifier:@"MyViewController"];
The foreground notification in AppDelegate:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationForegrounded object:self];
}
The foreground notification in the view controller:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resumeForeground) name:kNotificationForegrounded object:nil];
}
I tried creating a strong reference in the AppDelegate, but the view controller still gets deallocated:
@property (strong, nonatomic) MyViewController *myViewController;
I tried adding the view controller to an array and have a strong reference to the array in the AppDelegae, but still I get the same results:
@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];