Fully-loaded UIViewController losing all it's data after adding to scroll view

Posted by clozach on Stack Overflow See other posts from Stack Overflow or by clozach
Published on 2010-05-02T00:37:18Z Indexed on 2010/05/02 0:47 UTC
Read the original article Hit count: 558

Filed under:
|
|
|

Summary

I'm repurposing Apple's Page Control project. In loadScrollViewWithPage:, the view controllers that I'm adding to the scroll view appear on screen without their initialized values, as if displayed directly from the nib.

Specifics

Here's the code that appears to be working when I step through it:

 CBFullScreenViewController *controller = [viewControllers objectAtIndex:page];

 if ((NSNull *)controller == [NSNull null]) {
  controller = [[CBFullScreenViewController alloc] init];

  // Populate the view from the corresponding CBImage object
  CBImage *imageObject = [imageArray objectAtIndex:page];
  BOOL bookmarked = [imageObject.bookmarked boolValue];
  controller.bookmarkButton.highlighted = bookmarked;
  NSString *subtitle = imageObject.subtitle;
  controller.closedSubtitleLabel.text = subtitle;
     // <-- snip...more initialization --> //
  controller.delegate = self;

  [viewControllers replaceObjectAtIndex:page withObject:controller];
  [controller release];
 }

// add the controller's view to the scroll view if (nil == controller.view.superview) { CGRect frame = scrollView.frame; frame.origin.x = frame.size.width * page; frame.origin.y = 0; controller.view.frame = frame; [scrollView addSubview:controller.view];//<< controller and subviews } // all have non-null, seemingly // valid values at this point

Here's the init method in CBFullScreenViewController:

- (id)init {
 if ((self = [super initWithNibName:@"CBFullScreenViewController" bundle:nil])) {
  self.cover = [[UIImageView alloc] init];
  self.homeButton = [[UIButton alloc] init];
  self.tabView = [[UIButton alloc] init];
  self.closedSubtitleLabel = [[UILabel alloc] init];
  self.openSubtitleLabel = [[UILabel alloc] init];
    // <-- snip...more initialization --> //
 }
 return self;
}

While troubleshooting this for the last 2 hours has helped me track down some unrelated memory leaks, I can't for the life of me figure out what's happening to the values I'm putting into my view!

Oh, it's probably worth mentioning that I've got @synthesize for each of my outlets, and they're all hooked up in IB. Screenshot here.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone