Bluearms
Bluearms

Reputation: 5

Multiple View Controllers. Is there a maximum?

I am putting an iPad application together that allows a user to work their way through a virtual tour. They are able to move forward through screens on which some will have buttons to other material such as a video or more info.

If Keynote supported Hyperlinks then it would be well suited but as it doesn't I am trying to recreate the tour within Xcode.

I am a newbie but have spent time researching and have code to display the 'slides' and the capability to move forward and back through them. The slides are no more that an image view with a full screen graphic and buttons for the various options, some slides are simple and have nothing other than back and forward but others will have additional links

However doing it in this simplistic way means I am ending up with a huge number of view controllers and XIB files, currently at 75 which I know must be more than any app should have. However it does work although on occasions when running it on the device and not in the simulator it will bomb out.

My questions are is there a limit to the number of view controllers in one app and will having a large number cause the instability? I'm aware of other ways to handle the views such as having them in arrays and pushing them out a single view controller but this won't give me the flexibility to tailor slides for different content.

I'd welcome any help or advice and I hope have gone about posting this question in the right way (its my first)

Many Thanks Kieron

The code I am using to manipulate the view is

-(IBAction)goBack {
    [self dismissModalViewControllerAnimated:NO];
}

-(IBAction)goForward {  
    Slide5ViewController *screen = [[Slide5ViewController alloc] initWithNibName:nil bundle:nil];
    screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:screen animated:YES];
    [screen release];
}   

Upvotes: 0

Views: 2501

Answers (2)

Erdemus
Erdemus

Reputation: 2788

Fist of all, what error is in the log?

Did you properly implemented viewDidUnload method of view controllers? View controllers should be able to unload loaded xib. Also, release data in didReceiveMemoryWarning.

Second, it could be better to use UINavigationController to handle view controllers stack instead of modal view controllers stack. You can hide navigation bar or customize it.

Upvotes: 0

makdad
makdad

Reputation: 6460

Kieron,

Why not have one "slide" view controller and a different image only? Use some sort of data structure to keep information about the buttons, images, and pathways for each slide, and then just keep re-using the same view controller for each slide?

The view controller can then dynamically load each image as it transitions between the currently visible view and the next instantiation of itself... It should be possible using only 1 view controller.

If you're comfortable with using Interface Builder, keep using the XIB files to lay everything out. However, instead of setting each "File's Owner" to a different view controller, set them all to the same one. Then, inside your IBAction methods (when the user pressed a button), use some logic to say "I am on this view right now, and the user pressed this button, so which one should I go to next?"

Then, call a method like loadNewSlide: that might look like this:

- (void) loadNewSlide:(NSInteger)slideNumber
{
  // Make a string with the new XIB name
  NSString* xibName = [NSString stringWithFormat:@"slide-%d",slideNumber];

  // Create the next slide view controller (it doesn't matter if you create a slide view 
  // controller from within another slide view controller, remember, they are all just  
  // objects)
  SlideViewController *newSlideViewController = [[SlideViewController alloc] initWithNibName:xibName bundle:nil];

  // Change the view
  UIWindow *theWindow = [self.view superview];
  [self.view removeFromSuperview];
  [theWindow addSubview:newSlideViewController.view];

  // Release, the view stack now should be retaining the view controller instead
  [newSlideViewController release];
}

This will work MUCH better than running "modally" with 75 view controllers (as you had previously suggested) because this will only keep 1 slide in memory at a time - whatever you are currently looking at - and then will load the next slide just in time to move to it.

Upvotes: 4

Related Questions