Maintaining backwards capability?


In the interests of maintaining a reasonable level of backwards compatibility (and because my Touch is only a 1st gen), I try to make sure all my code works back to 3.1.3, which I have managed with only very minor tweaks up until this chapter. Now though, I am stuck with getting autorotation to work. Making the recommended change in didFinishLaunchingWithOptions seems to stop notifications getting through in the simulator. The 4.0 version crashes on a 3.1.3 device, as expected (no setRootViewController selector).

4.0 :

HeavyViewController *hvc = [[[HeavyViewController alloc] init] autorelease]; [[self window] setRootViewController:hvc];


HeavyViewController *hvc = [[[HeavyViewController alloc] init] autorelease]; [self.window addSubview: [hvc view]];

At this point in my explanation, I’ve just remembered an issue experienced a couple of chapters ago and also the solution. I’ve decided to continue with the post to preserve the information, should anybody else benefit from it.

setRootViewController retains the view, so the autorelease does the right thing. addSubView doesn’t retain the view, so the autorelease causes the HeavyViewController pointed to by hvc to be deallocated. Somehow I was getting away with it not causing a crash in this instance.

3.1.3, correct version:

HeavyViewController *hvc = [[HeavyViewController alloc] init]; [self.window addSubview: [hvc view]];

p.s. Note to moderator - preview currently causes half a dozen phpbb debug messages to be printed at the top of a page.


Thanks this helped. I was on Chapter 7 (View Controllers) and needed to see this.

My iPhone 3G was running on 3.1.3 and the code in the book (and iPhone Simulator) was for iOS 4.0 color=#FF0000.

I found the code [color=#FF0000] self.window addSubview:[/color] on the internet but it didnt quite work in my problem context. But I saw your code and got confirmation I was on the right track atleast.

So it should have worked but it didnt.
Then I released it was the[color=#FF0000] [tabBarController release][/color] code.
Since the View Controller[color=#FF0000] tabBarController[/color] is not retained by [color=#FF0000]window.[/color] I shouldnt [color=#FF0000]release[/color] the view controller [color=#FF0000] tabBarController[/color].

I removed [color=#FF0000] [tabBarController release][/color] from HypnoTimeDelegate.m code given in the book and the App works fine on my iOS 3.1.3 loaded iPhone.

hope this helps others.