uiscrollview not switching image subviews

Posted by nickthedude on Stack Overflow See other posts from Stack Overflow or by nickthedude
Published on 2010-03-17T03:51:53Z Indexed on 2010/03/17 5:41 UTC
Read the original article Hit count: 441

I'm building a comic viewer app, that consists of two view controllers, the root viewcontroller basically displays a view where a user decides what comic they want to read by pressing a button. The second viewController actually displays the comic as a uiscrollview with a toolbar and a title at the top.

So the problem I am having is that the comic image panels themselves are not changing from whatever the first comic you go to if you select another comic after viewing the first one. The way I set it up, and I admit it's not exactly mvc, so please don't hate, anyway the way I set it up is each comic uiscrollview consists of x number of jpg images where each comic set's image names have a common prefix and then a number like 'funny1.jpg', 'funny2.jpg', 'funny3.jpg' and 'soda1.jpg', 'soda2.jpg', 'soda3.jpg', etc...

so when a user selects a comic to view in the root controller it makes a call to the delegate and sets ivars on instances of the comicviewcontroller that belongs to the delegate (mainDelegate.comicViewController.property) I set the number of panels in that comic, the comic name for the title label, and the image prefix.

The number of images changes(or at least the number that you can scroll through), and the title changes but for some reason the images are the same ones as whatever comic you clicked on initially.

I'm basing this whole app off of the 'scrolling' code sample from apple.

I thought if I added a viewWillAppear:(BOOL) animated call to the comicViewController everytime the user clicked the button that would fix it but it didn't, after all that is where the scrollview is laid out.

Anyway here is some code from each of the two controllers:

RootController:

-(IBAction) launchComic2{

AppDelegate *mainDelegate = [(AppDelegate *) [UIApplication sharedApplication] delegate]; mainDelegate.myViewController.comicPageCount = 3; mainDelegate.myViewController.comicTitle.text = @"\"Death by ETOH\""; mainDelegate.myViewController.comicImagePrefix = @"etoh";

[mainDelegate.myViewController viewWillAppear:YES]; [mainDelegate.window addSubview: mainDelegate.myViewController.view];

comicViewController:

   -(void) viewWillAppear:(BOOL)animated {

self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];

// 1. setup the scrollview for multiple images and add it to the view controller // // note: the following can be done in Interface Builder, but we show this in code for clarity [scrollView1 setBackgroundColor:[UIColor whiteColor]]; [scrollView1 setCanCancelContentTouches:NO]; scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite; scrollView1.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview scrollView1.scrollEnabled = YES;

// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo // if you want free-flowing scroll, don't set this property. scrollView1.pagingEnabled = YES;

// load all the images from our bundle and add them to the scroll view NSUInteger i; for (i = 1; i <= self.comicPageCount; i++) {

NSString *imageName = [NSString stringWithFormat:@"%@%d.jpg", self.comicImagePrefix, i]; NSLog(@"%@%d.jpg", self.comicImagePrefix, i); UIImage *image = [UIImage imageNamed:imageName]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList" CGRect rect = imageView.frame; rect.size.height = kScrollObjHeight; rect.size.width = kScrollObjWidth; imageView.frame = rect; imageView.tag = i; // tag our images for later use when we place them in serial fashion [scrollView1 addSubview:imageView]; [imageView release]; }

[self layoutScrollImages]; // now place the photos in serial layout within the scrollview

 }


  - (void)layoutScrollImages
 {

UIImageView *view = nil; NSArray *subviews = [scrollView1 subviews];

// reposition all image subviews in a horizontal serial fashion CGFloat curXLoc = 0; for (view in subviews) { if ([view isKindOfClass:[UIImageView class]] && view.tag > 0) { CGRect frame = view.frame; frame.origin = CGPointMake(curXLoc, 0); view.frame = frame;

curXLoc += (kScrollObjWidth); } }

// set the content size so it can be scrollable [scrollView1 setContentSize:CGSizeMake((self.comicPageCount * kScrollObjWidth), [scrollView1 bounds].size.height)]; }

Any help would be appreciated on this.

Nick

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone