<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>bytesizecreations &#187; viewcontroller</title>
	<atom:link href="http://www.bytesizecreations.com/category/viewcontroller/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bytesizecreations.com</link>
	<description>Musings from a busy mind</description>
	<lastBuildDate>Sun, 14 Jun 2009 17:44:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Working with Orientation Changes on the iPhone</title>
		<link>http://www.bytesizecreations.com/2009/05/working-with-orientation-changes-on/</link>
		<comments>http://www.bytesizecreations.com/2009/05/working-with-orientation-changes-on/#comments</comments>
		<pubDate>Fri, 01 May 2009 15:43:00 +0000</pubDate>
		<dc:creator>Michael Gaylord</dc:creator>
				<category><![CDATA[auto-rotation]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[iphone sdk]]></category>
		<category><![CDATA[orientation changes]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[sample code]]></category>
		<category><![CDATA[viewcontroller]]></category>

		<guid isPermaLink="false">http://www.42restaurants.com/byte/?p=49</guid>
		<description><![CDATA[Update: It looks like Apple has change the way shouldAutoRotateToInterfaceOrientation works in iPhone SDK 3.0. Here is my updated post on how to do this differently.
One of the features with which I had to get fairly intimate with on the iPhone over the last couple of months was working with orientation changes. At first my [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-family: Georgia; font-size: 14px;"><strong>Update:</strong> It looks like Apple has change the way shouldAutoRotateToInterfaceOrientation works in iPhone SDK 3.0. Here is my <a href="http://www.bytesizecreations.com/2009/06/working-with-orientation-changes-on.html">updated post</a> on how to do this differently.</span></p>
<p><span style="font-family: Georgia; font-size: 14px;">One of the features with which I had to get fairly intimate with on the iPhone over the last couple of months was working with orientation changes. At first my code was completely incorrect and it wasn’t immediately obvious to me how to get my application to reorient itself properly. This was mainly due to the structure of my code. In this post I am going to explain a simple, memory efficient way of working with</span> <span style="font-family: Courier;">UIViewController</span> <span style="font-family: Georgia; font-size: 14px;">and interface orientation changes.</span></p>
<p><span style="font-family: Georgia; font-size: 14px;"><strong>1. The Basics</strong></span></p>
<p><span style="font-family: Georgia; font-size: 14px;">Before we get into the nitty gritty let’s first have a recap of how interface orientation changes work and how you should respond to them. Firstly, if you want your view to auto-rotate you need to override the method</span> <span style="font-family: Courier;">shouldAutorotateToInterfaceOrientation</span><span style="font-family: Georgia; font-size: 14px;">: and</span> <span style="font-family: Courier;">return YES</span><span style="font-family: Georgia; font-size: 14px;">. If you want to only allow auto-rotation under a certain condition you can put the test for this condition in this method too.</span></p>
<p><span style="font-family: Georgia; font-size: 14px;">This is pretty much the most basic thing you need to do to allow for auto-rotation but there are additional methods you can override that can be very useful. These methods are</span></p>
<p><span style="font-family: Courier;"><code>• willRotateToInterfaceOrientation</code></span></p>
<p><span style="font-family: Courier;"><code>• didRotateFromInterfaceOrientation</code></span></p>
<p><span style="font-family: Courier;"><code>• willAnimateFirstHalfOfRotationToInterfaceOrientation</code></span></p>
<p><span style="font-family: Courier;"><code>• willAnimateSecondHalfOfRotationFromInterfaceOrientation</code></span></p>
<p><span style="font-family: Georgia; font-size: 14px;">The first 2 methods are very useful for doing pre- and post-processing of your rotation. You could perhaps initialize a view controller or add some views to your current view in the</span> <span style="font-family: Courier;">willRotateToInterfaceOrientation</span><span style="font-family: Georgia; font-size: 14px;">. The second 2 are pretty much self explanatory. If you want to perform additional operations during that particular phase of the rotation you can implement them too.</span></p>
<p><span style="font-family: Georgia; font-size: 14px;">Another very useful code example for when working with view controller orientations is:</span></p>
<pre>
<span style="font-family: Courier;"><code>if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
    //do some processing…
}else if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
    //do different processing…}
else if(UIDeviceOrientationIsValidInterfaceOrientation(interfaceOrientation)){
    //do something
}</code></span>
</pre>
<p></p>
<p><span style="font-family: Georgia; font-size: 14px;">Most programmers aren’t aware of these macros and they do cut down and simplify code.</span></p>
<p><span style="font-family: Georgia; font-size: 14px; font-weight: bold;">2. Loading &amp; Unloading View Controllers</span></p>
<p><span style="font-family: Georgia; font-size: 14px;">To save on memory usage you can load and unload view controllers as your interface orientation changes. I found this incredibly useful as my application required a fair amount of memory. It basically works on the principle that the application’s delegate class will manage the changing of the view controllers.</span></p>
<p><span style="font-family: Georgia; font-size: 14px;">In you application delegate you can add the following method:</span></p>
<pre>
<span style="font-family: Courier;"><code>- (void) switchViewControllers:(UIViewController *)controller{
if(self.viewController != nil){
    [viewController.view removeFromSuperview];
}
    self.viewController = controller;
    [window addSubview:self.viewController.view];
}</code></span>
</pre>
<p></p>
<p><span style="font-family: Georgia; font-size: 14px;">Make sure you have a</span> <span style="font-family: Courier;">viewController</span> <span style="font-family: Georgia; font-size: 14px;">property on the application delegate of course.</span></p>
<p><span style="font-family: Georgia; font-size: 14px;">When</span> <span style="font-family: Courier;">switchViewControllers</span> <span style="font-family: Georgia; font-size: 14px;">is called, your currently visible view is removed from its superview and the new view controller and its associated view are added.</span></p>
<p><span style="font-family: Georgia; font-size: 14px;">The next step is to add this piece of code inside your view controllers that you want to switch out during auto rotation:</span></p>
<pre>
<span style="font-family: -webkit-monospace;"><code>- (BOOL)shouldAutorotateToInterfaceOrientation:
                (UIInterfaceOrientation)interfaceOrientation {
    CustomUIViewController *customUIViewController =
                [[CustomUIViewController alloc] init];
    MyAppDelegate *appDelegate =
        (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate switchViewControllers: customUIViewController];
    [customUIViewController release];
}</code></span>
</pre>
<p></p>
<p><span style="font-family: Georgia; font-size: 14px;">Where</span> <span style="font-family: Courier;">CustomUIViewController</span> <span style="font-family: Georgia; font-size: 14px;">is your own view controller you want to switch to.</span></p>
<p><span style="font-family: Georgia; font-size: 14px;"><strong>3. Pitfalls</strong></span></p>
<p><span style="font-family: Georgia; font-size: 14px;">This approach has worked very well for me but I did have an issue where I was using 3 view controllers. Let’s call them A, B and C. A switched to B and vice-versa when the phone’s orientation changed. C was loaded by B and was viewable in landscape and portrait orientations. The problem came in where if you changed C’s orientation and then the user navigated back to B where B had a different orientation, you would effectively not be notified of an orientation change. If you subsequently changed B’s orientation to load A, you would find A would load in the wrong orientation. Oops, bug…</span></p>
<p><span style="font-family: Georgia; font-size: 14px;">After trolling a number forums I discovered there used to be an undocumented call in the SDK called</span> <span style="font-family: Corbel;">changeOrientation</span><span style="font-family: Georgia; font-size: 14px;">: but for some or other reason Apple removed this method.</span></p>
<p><span style="font-family: Georgia; font-size: 14px;">So how did I solve this? Well all I did was put this piece of code in A so that it would manually reorient itself from portrait to landscape:</span></p>
<pre>
<span style="font-family: Courier;"><code>- (void)viewDidAppear:(BOOL)animated {
    if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation)){
        [UIView beginAnimations:@”View Flip” context:nil];
        [UIView setAnimationDuration:0.5f];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        self.view.transform = CGAffineTransformIdentity;
        self.view.transform =
            CGAffineTransformMakeRotation(MPI * (90) / 180.0);
        self.view.bounds = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f);
        self.view.center = CGPointMake(160.0f, 240.0f);
        [UIView commitAnimations];
    }
}</code></span>
</pre>
<p></p>
<p><span style="font-family: Georgia; font-size: 14px;">Conversely you can use this piece of code to reorient from landscape to portrait if your “A” view controller needs to be in portrait mode:</span></p>
<pre>
<span style="font-family: Courier;"><code>- (void)viewDidAppear:(BOOL)animated {
    if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
        [UIView beginAnimations:@”View Flip” context:nil];
        [UIView setAnimationDuration:0.5f];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        self.view.transform = CGAffineTransformIdentity;
        self.view.transform =
            CGAffineTransformMakeRotation(MPI * (90) / 180.0);
        self.view.bounds = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
        [UIView commitAnimations];
    }
}</code></span>
</pre>
<p></p>
<p><span style="font-family: Georgia; font-size: 14px;">So far this method of loading and unloading view controllers on orientation changes has proved to be very memory efficient and not too performance intensive either. Let me know in the comments if it works for you or if you have any ideas about this method of managing view controllers and orientation changes.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bytesizecreations.com/2009/05/working-with-orientation-changes-on/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
