Wrox Programmer Forums
|
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 June 12th, 2011, 07:11 PM
Authorized User
 
Join Date: May 2011
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The "pageTurning" method does three things:

(1) the switch statement examines the updated "nextPage" and assigns the relevant image to "tempImageView"

(2) the if-then-else statement ping pongs "tempImageView" and "bgImageView"

(3) the two sets of animation statements flip between "imageView1" and "imageView2" via proxies "tempImageView" and "bgImageView"

If this is not clear, I'll layout a step-by-step sequence of two calls to "pageTurning" for you.
 
Old June 12th, 2011, 07:43 PM
Authorized User
 
Join Date: Jun 2011
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Frank. I am still stuck on the pingponging bit. Please bare with me and leave out all talk of animation for the time being.
I can run the program with full functionality (without animation) like this:
Code:
- (void) pageTurning: (UIPageControl *) pageController 
{	
    NSInteger nextPage = [pageController currentPage];   
        switch (nextPage) {                           
        case 0:
            [tempImageView setImage:
			 [UIImage imageNamed:@"iMac_old.jpeg"]];
            break;
        case 1:
            [tempImageView setImage:
			 [UIImage imageNamed:@"iMac.jpeg"]];
            break;
        case 2:
            [tempImageView setImage:
			 [UIImage imageNamed:@"Mac8100.jpeg"]];
            break;
        case 3:
            [tempImageView setImage:
			 [UIImage imageNamed:@"MacPlus.jpeg"]];
            break;
        case 4:
            [tempImageView setImage:[UIImage imageNamed:@"MacSE.jpeg"]];
            break;
        default:
            break;
    }
    if (tempImageView.tag == 0) { bgImageView = imageView1; }
    else { bgImageView = imageView2; }	
    [bgImageView setHidden:NO];
}
I know that the first time we enter this loop, imageView1 is filled with 'iMac_old.jpeg' from the DidLoad Routine. Now we step one page forward, current page is now one and tempImageView is filled with 'iMac.jpeg'. The tag is NOT zero (Why?) so we enter the 'else' condition. This means bgImageView is filled with imageView2, which is, as yet unused/empty. Why are we seeing a new picture? 'iMac.jpeg' is just sitting in the tempImageView at this point, isn't it?
 
Old June 12th, 2011, 07:49 PM
Authorized User
 
Join Date: May 2011
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

"The tag is NOT zero (Why?) so we enter the 'else' condition"

Ah. Step 4 on page 54 sez "with the first ImageView selected, open the Attributes Inspector window and set the Tag property to 0. Select the second ImageView and set the Tag property to 1 (see Figure 3-14)"

That is what facilitates the ping-pong. Each time "pageTurning" is entered, the "tag" member of the currently selected ImageView will trigger a ping [or pong] to the other ImageView.
 
Old June 12th, 2011, 08:43 PM
Authorized User
 
Join Date: Jun 2011
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok I get what's going on with the tags. But this is still animation related and therefor distracting from my original question. In my XIB I have two views hardwired to imageView1 and 2. when I put something in there and un-hide it, I can see it on the screen - understood.

But the switch and if/then loops don't put anything in there! They put pictures into tempImageView and bgImageView, but never into the imageView1 and 2. That's the bit I can't get my head round. Where is the instruction putting one of the jpgs into one of the numbered (visible) imageViews?

yet more simplified code:
Code:
- (void) pageTurning: (UIPageControl *) pageController 
{	
    NSInteger nextPage = [pageController currentPage];   
        switch (nextPage) {                           
        case 0:
            [tempImageView setImage:
			 [UIImage imageNamed:@"iMac_old.jpeg"]];
            break;
        case 1:
            [tempImageView setImage:
			 [UIImage imageNamed:@"iMac.jpeg"]];
            break;
        case 2:
            [tempImageView setImage:
			 [UIImage imageNamed:@"Mac8100.jpeg"]];
            break;
        case 3:
            [tempImageView setImage:
			 [UIImage imageNamed:@"MacPlus.jpeg"]];
            break;
        case 4:
            [tempImageView setImage:[UIImage imageNamed:@"MacSE.jpeg"]];
            break;
        default:
            break;
    }
    bgImageView = imageView2;
    [bgImageView setHidden:NO];
}
 
Old June 12th, 2011, 09:33 PM
Authorized User
 
Join Date: May 2011
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

tempImageView is a pointer to an ImageView object, not an object in its own right. Thus the switch statement cases, when they invoke method setImage indirect tempImageView, are actually setting the JPG in either ImageView1 or ImageView2.

Then the if-then-else ping-pongs them.

You're probably not impressed with the Objective C syntax of "messaging" with the square brackets. Had this been C++, you would have seen a tempImageView->setImage and it would have been more obvious.
 
Old June 12th, 2011, 09:35 PM
Friend of Wrox
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

This misunderstanding has already been addressed in the forum
Views Controller question see my January 10th posting.

In brief you are not assigning images, you are assigning pointers - memory addresses. if you insert the following NSLog statements it should be apparent what is going on.

Code:
- (void)viewDidLoad {	
	
	//---initialize the first imageview to display an image---
    [imageView1 setImage:[UIImage imageNamed:@"iMac_old.jpeg"]];
	NSLog(@"imageView1 is %p The tag is %d",imageView1, imageView1.tag);
	
    tempImageView = imageView2;
	NSLog(@"tempImageView is %p and imageView2 is %p",tempImageView, imageView2);
	
    //---make the first imageview visible and hide the second---
    [imageView1 setHidden:NO];
    [imageView2 setHidden:YES];
	
    //---add the event handler for the page control---
    [pageControl addTarget:self
                    action:@selector(pageTurning:)
          forControlEvents:UIControlEventValueChanged];
 		
    [super viewDidLoad];
}

//---when the page control’s value is changed---
- (void) pageTurning: (UIPageControl *) pageController {	
    //---get the page number you can turning to---
    NSInteger nextPage = [pageController currentPage];
    switch (nextPage) {
        case 0:
            [tempImageView setImage:
			 [UIImage imageNamed:@"iMac_old.jpeg"]];
			NSLog(@"tempView is %p. imageView2 is %p", tempImageView, imageView2);
			NSLog(@"tempView is %p. imageView1 is %p", tempImageView, imageView1);
            break;
        case 1:
            [tempImageView setImage:
			 [UIImage imageNamed:@"iMac.jpeg"]];
			NSLog(@"tempView is %p. imageView2 is %p", tempImageView, imageView2);
			NSLog(@"tempView is %p. imageView1 is %p", tempImageView, imageView1);
            break;
        case 2:
            [tempImageView setImage:
			 [UIImage imageNamed:@"Mac8100.jpeg"]];
			NSLog(@"tempView is %p. imageView2 is %p", tempImageView, imageView2);
			NSLog(@"tempView is %p. imageView1 is %p", tempImageView, imageView1);
            break;
        case 3:
            [tempImageView setImage:
			 [UIImage imageNamed:@"MacPlus.jpeg"]];
			NSLog(@"tempView is %p. imageView2 is %p", tempImageView, imageView2);
			NSLog(@"tempView is %p. imageView1 is %p", tempImageView, imageView1);
            break;
        case 4:
            [tempImageView setImage:
			 [UIImage imageNamed:@"MacSE.jpeg"]];
			NSLog(@"tempView is %p. imageView2 is %p", tempImageView, imageView2);
			NSLog(@"tempView is %p. imageView1 is %p", tempImageView, imageView1);
            break;
        default:
            break;
    }
	
    //---switch the two imageview views---
	NSLog(@"Temp imageView  tag is %d",tempImageView.tag);
    if (tempImageView.tag == 0) { //---imageView1---
		NSLog(@"Tag is 0");
        tempImageView = imageView2;
        bgImageView = imageView1;
    }
    else {    //---imageView2---
        tempImageView = imageView1;
        bgImageView = imageView2;
		NSLog(@"In else. tempImageView is %p and bgImageView is %p", tempImageView, bgImageView);
    }
	
    //---animate the two views flipping---
    [UIView beginAnimations:@"flipping view" context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
                           forView:tempImageView
                             cache:YES];
	
    [tempImageView setHidden:YES];
    
	[UIView commitAnimations];
	
    [UIView beginAnimations:@"flipping view" context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView
	 setAnimationTransition:UIViewAnimationTransitionFlipFromRight
	 forView:bgImageView
	 cache:YES];
    
	[bgImageView setHidden:NO];
    
	[UIView commitAnimations];	
}
If tempImageView and imageView1 are the same pointer then any image set in tempImageView is assigned to ImageView1, since they are pointing to the same address in memory, likewise if tempImageView and imageView2 are the same pointer. The assignment in the if/else is simply exchanging the two pointers to the imageViews.
 
Old June 13th, 2011, 04:42 PM
Authorized User
 
Join Date: Jun 2011
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
If tempImageView and imageView1 are the same pointer then any image set in tempImageView is assigned to ImageView1, since they are pointing to the same address in memory, likewise if tempImageView and imageView2 are the same pointer. The assignment in the if/else is simply exchanging the two pointers to the imageViews.
Ok, this I understand now thanks.

So only address pointers are being ping-ponged around, not images.
By the way, is there a way to reveal the name of the image currently stored in a given view? and display its name in NSLog, for example?
 
Old June 13th, 2011, 08:55 PM
Authorized User
 
Join Date: May 2011
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I believe there is a getImage that is the reverse of setImage. With that, you can get the parameters of the image in the view object.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Loading Image to Image Control using JavaScript ctranjith General .NET 1 May 28th, 2013 05:43 AM
Chapter 3 Page Control & Image Views programmer2010 BOOK: Beginning iPad Application Development 10 January 12th, 2011 09:06 AM
Ch3 Page Control Image View nauticalmac BOOK: Beginning iPad Application Development 0 August 24th, 2010 06:59 AM
View of image fields AlexSepahpour SQL Server 2005 0 October 24th, 2007 06:06 PM





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