I'm trying to create a class cluster as subclass of UIViewController to accomplish some points:
1. Different behavior of the ViewController depending on actual iOS version
2. iOS version checks don't clutter up the code
3. Caller doesn't need to care
So far I got the classes MyViewController, MyViewController_iOS7 and MyViewController_Legacy.
To create instances I call the method myViewControllerWithStuff:(StuffClass*)stuff which is implemented like:
+(id)myViewControllerWithStuff:(StuffClass*)stuff
{
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1)
{
return [[MyViewController_iOS7 alloc] initWithStuff:stuff];
}
else
{
return [[MyViewController_Legacy alloc] initWithStuff:stuff];
}
}
The caller uses myViewControllerWithStuff:. After that the so created view controller gets pushed onto a UINavigationController's navigation stack.
This nearly works as intended with one big downside: ARC doesn't dealloc the instance of MyViewController_xxx when it gets popped from the navigation stack. Doesn't matter which iOS version.
What am I missing?
UPDATE: -initWithStuff:
-(id)initWithStuff:(StuffClass*)stuff
{
if (self = [super init])
{
self.stuff = stuff;
}
return self;
}
This method is also implemented in MyViewController. The differences kick in later (e.g. viewDidLoad:).
First of all: Thanks for all your help, comments, answers, suggestions...
Of course there was another
strongreference to theMyViewController-object. But it wasn't that obvious, because it was not a property or instance variable.In
viewDidLoadI did the following:This should prevent data loss in case the user sends the app to the background. Of course the block captures the needed parts of its environment. In this case it captured
self. The block keepsselfalive until it (the block) gets destroyed, which is the case when e.g.[[NSNotificationCenter defaultCenter] removeObserver:self];gets called. But, bad luck, this call is placed in thedeallocmethod ofMyViewControllerwhich won't get called as long as the block exists...The fix is as follows:
Now the block captures
weakSelf. This way it can't keep theMyViewController-object alive and everything deallocs and works just fine.