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 December 10th, 2010, 11:25 AM
Registered User
 
Join Date: Dec 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to BrooklynDon
Default Views Controller question

I've been using the iOS application Development book nicely, but I came upon a roadblock with the Page Control and Image View example. I keep getting the first picture every time. Here's the header Code:

Code:
//
//  Using_ViewsViewController.h
//  Using Views
//
//  Created by Don Stankalis on 12/10/10.
//  Copyright 2010 Downstate Medical Center. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface Using_ViewsViewController : UIViewController {
	IBOutlet UIPageControl *pageControl;
	IBOutlet UIImageView *imageView1;
	IBOutlet UIImageView *imageView2;
	UIImageView *tempImageView, *bgImageView;
	
}

@property (nonatomic,retain) UIPageControl *pageControl;
@property (nonatomic,retain) UIImageView *imageView1;
@property (nonatomic,retain) UIImageView *imageView2;

@end
and here is the main code:
Code:
//
//  Using_ViewsViewController.m
//  Using Views
//
//  Created by Don Stankalis on 12/10/10.
//  Copyright 2010 Downstate Medical Center. All rights reserved.
//

#import "Using_ViewsViewController.h"

@implementation Using_ViewsViewController

@synthesize pageControl;
@synthesize imageView1,imageView2;

- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex{
	NSLog(@"%d",buttonIndex);
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	//--initialize the first imageview todisplay an image--
	[imageView1 setImage:[UIImage imageNamed:@"iMac_old.jpeg"]];
	tempImageView = imageView2;
	
	//--make the first imageview visable 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"]];
			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;
	}
	
	//--switch the two image views--
	if (tempImageView.tag == 0) {//--imageView1--
		tempImageView = imageView2;
		bgImageView = imageView1;
	}
	else {//--imageView2--
		tempImageView = imageView1;
		bgImageView = imageView2;
	}
	
	//--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];
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[pageControl release];
	[imageView1 release];
	[imageView2 release];
    [super dealloc];
}

@end
hmmmm.... Thanks in advance

Don Stankalis
Web Development, SUNY Downstate Medical Center
 
Old January 9th, 2011, 04:24 PM
Friend of Wrox
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

The same project was used in an earlier book by the author, and a discussion of some errors made is here:
Chapter 3 Page Control & Image Views
The problem is most likely in Interface Builder. If you can't resolve the issue let me know, or send the project file to
ipadhelper@me.com
and I will check it out.
Bob
 
Old January 9th, 2011, 10:55 PM
Registered User
 
Join Date: Jan 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default I encountered the same issue, and here is how I solved it:

In the pageTurning method, the setImage instructions are storing the next jpeg image into the tempImageView class. However, the switch imageview views section is then overlaying the contents of tempImageView with that of imageView2, which was initialized previously (probably with null) at the beginning of the viewDidLoad method and the initial image stored in imageView1 is then stored into bgImageView.

It is the same image that is currently being diaplayed! Therefore it doesn't appear to change images at all.

My solution was to change all the instances of tempImageView in the pageTurning method to 'imageView1'. Therefore in the switch section, the next jpeg is allowed to be stored in the bgImageView and then the animation routine has something different to display.
 
Old January 10th, 2011, 10:33 AM
Friend of Wrox
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

I am glad that you got the program to work, but if you had to change the code that means that there is an error somewhere. I would verify in Interface Builder the tags and connections. A good way to check out everything is shrink the UIImageViews so that one occupies the upper left quadrant, and the other the lower right, and add a label so that you know which is which.
A walkthrough-

-(void)viewDidLoad

imageView1 (tag = 0) is set to the first image to be displayed.

tempImageView is set to the address of imageView2, with tag=1. Remember, they are pointers.

imageView1 is now displayed, and imageView2 is hidden.

-(void)pageTurning

Assume you go to case 1:
iMac.jpeg is assigned to tempImageView, which is the same as assigning it to imageView2 since they are the same pointer.

Since the tag of tempImageView is 1 (it is still a pointer to imageView2, tag=1), flow goes to the else statement. In the else statement tempImageView is reassigned with a pointer to imageView1.
tempImageView = imageView1;

imageView2 and tempImageView now point to two different addresses. imageView2 is currently "iMac.jpeg" and tempImageView is "iMac_old.jpeg" (the same as imageView1)

bgImageView is now assigned the pointer imageView2 with its current content "iMac.jpeg"
bgImageView = imageView2;

tempImageView is now hidden and bgImageView shown using the transition animation.

When the step repeats with a new page turn, the image assigned in the switch statement to tempImageView is also being assigned to imageView1, since they are the same pointer. Since tempImageView is pointing to imageView1 (tag = 0), the if part of the the if…else will be executed. tempView is reassigned to the pointer imageView2 and bgImageView the pointer to imageView1.

Bob
 
Old January 10th, 2011, 02:06 PM
Registered User
 
Join Date: Dec 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to BrooklynDon
Default Thanks

Thanks for the replies . I'll try them


-Don
 
Old January 11th, 2011, 06:55 PM
Registered User
 
Join Date: Jan 2011
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
Default Simple Solution

It all depends on the order of you view in the Interface Builder. More precisely it depends on the order of the tag values 0, 1 or 1, 0. You can either reverse the tag values in your views or simply change the "if" statement from a "0" to a "1"

If (tempImageView.tag == 0)

to

If (templImageView.tag ==1)

Basically, this is an alternating switch and it was expecting the "0" first. If you put "1" first then "0" will never appear in the "if" statement and the picture will never change.

Hope this helps!
The Following User Says Thank You to windcheetah For This Useful Post:
ghestrada (January 18th, 2011)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Unit test for Controller question. RegisterRR BOOK: Testing ASP.NET Web Applications 0 March 22nd, 2010 06:29 PM
Shouldn't they be in a controller? phpcoder101 BOOK: Professional CodeIgniter ISBN: 978-0-470-28245-8 2 November 6th, 2009 05:24 PM
Controller Question susca BOOK: Professional CodeIgniter ISBN: 978-0-470-28245-8 1 July 6th, 2009 06:09 PM
controller to controller tigransh Spring 1 November 11th, 2006 12:48 AM
converting Access 2000 views to Sql views matta Classic ASP Professional 1 January 26th, 2005 03:37 PM





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