Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Mobile Development > BOOK: Beginning iOS 4 Application Development
BOOK: Beginning iOS 4 Application Development
This is the forum to discuss the Wrox book Beginning iOS 4 Application Development by Wei-Meng Lee; ISBN: 978-0-470-91802-9
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning iOS 4 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 4th, 2012, 09:45 PM
Wrox Author
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default Chapter 7 - Responding to Device Rotations - Erroneous code

Greetings everyone.

I am currently working through the Beginning iOS 4 Application Development book and have come across a problem with the second Try It Out in Chapter 7.

-(void) positionViews 
     UIInterfaceOrientation destOrientation = self.interfaceOrientation; 
if (destOrientation == UIInterfaceOrientationPortrait || destOrientation == UIInterfaceOrientationPortraitUpsideDown) { //—-if rotating to portrait mode—- btn.frame = CGRectMake(20, 20, 233, 37); } else { //—-if rotating to landscape mode—- btn.frame = CGRectMake(227, 243, 233, 37); }
The offending code is in bold. If you run the code sample as is provided in the book (I did not download the samples provided for the book so I am not sure if they are correct) when you rotate the simulator into landscape mode the button will stay at position 20,20 and then when you rotate the simulator back to portrait mode the button moves to 227,243! Basically this is happening is because self.interfaceOrientation contains the Orientation of the previous view!

The reason this is happening, I believe, is because positionViews is being called to early. There are two possible solutions to this. The first would be to call positionViews like this:

-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    [self positionViews];
The only problem with this is that it will actually animate the views as they reposition themselves which I found to be highly annoying. This was my solution to correct the problem (relevant code only from ScreenRotationsViewController.m displayed):
-(void)positionViews:(UIInterfaceOrientation) currentOrientation {
    UIInterfaceOrientation destOrientation = currentOrientation; 
    if (destOrientation == UIInterfaceOrientationPortrait || destOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        btn.frame = CGRectMake(20, 20, 233, 37);
        btn.frame = CGRectMake(227, 243, 233, 37);

- (void)willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation
                                duration:(NSTimeInterval) duration 
    [self positionViews:toInterfaceOrientation];

- (void)viewDidLoad
    [self positionViews:self.interfaceOrientation];
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
The magic is in the bolded code above: toInterfaceOrientation contains the orientation that the simulator is going to which then permits the code in positionViews to execute correctly.

If it matters I am running Xcode v4.2 with my deployment target set to 5.0 (I have not downloaded the latest Beta yet).

If anyone has any questions please let me know.


edit: I have also submitted this to the errata.
edit 2: This is for the iOS 5 version of the book that was published in January of 2012.
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."

Last edited by dparsons; February 4th, 2012 at 09:57 PM.. Reason: Submitted to errata.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Latest edition missing Chapter 18 - Device Drivers pchandra BOOK: Beginning Linux Programming, 4th Edition ISBN 978-0-470-14762-7 3 August 22nd, 2011 02:58 PM
Ch. 20 - Compass & Rotations not working sirab33 BOOK: Beginning iOS 4 Application Development 2 June 6th, 2011 11:15 PM
Error: Erroneous code error46146 BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 July 17th, 2005 05:35 PM
Chapter 14 Responding to clicks tfackler BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 2 February 1st, 2005 11:40 AM
Code stops responding when breakpoints added jaucourt VB.NET 2002/2003 Basics 0 September 2nd, 2004 03:40 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.