Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Mobile Development > BOOK: Beginning iOS 4 Application Development
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 4th, 2012, 08:45 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
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.

Code:
-(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:

Code:
-(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):
Code:
-(void)positionViews:(UIInterfaceOrientation) currentOrientation {
    UIInterfaceOrientation destOrientation = currentOrientation; 
    if (destOrientation == UIInterfaceOrientationPortrait || destOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        btn.frame = CGRectMake(20, 20, 233, 37);
    }
    else
    {
        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.

-Doug

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 08:57 PM. Reason: Submitted to errata.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 10:40 AM
Code stops responding when breakpoints added jaucourt VB.NET 2002/2003 Basics 0 September 2nd, 2004 03:40 AM



All times are GMT -4. The time now is 11:09 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.