Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Development: How can I preload a view controller before pushing it onto the navigation stack?

I'm diving into iOS development and I'm building a puzzle game to help me become familiar with the platform. I have AdWhirl displaying ads in the score screen that is displayed when the user completes a puzzle. The problem is, it takes at least a few seconds for the score screen view controller to request and receive the ad, in which time the user looks at the score and moves onto a different view. I'm planning to implement an animation that occurs when the user solves a puzzle and the time it takes for the animation to finish would be a good time to request and receive the ad that will be displayed in the next (score) view the user will be taken too.

During the time that the solved-the-puzzle animation is taking place, how can I preload the next view controller so that the ad is present when I push the view controller on to the navigation stack? If this isn't possible, or if it's a bad idea, do you have any suggestions for how I can request and receive the ad while the animation is taking place?

Thanks so much in advance for your wisdom!

Note: For those who aren't familiar with AdWhirl, the process of requesting and receiving an ad is simple. In the viewDidLoad method of the view controller you want the ad to appear in, you create an AdWhirlView (subclass of UIView), add it as subview, and call a method that requests an ad. When the ad arrives, it calls a delegate method to display it in the parent view.

like image 379
BeachRunnerFred Avatar asked Dec 19 '10 18:12

BeachRunnerFred


People also ask

How do I add a view controller to my navigation controller?

Step 1: Embed root view controller inside a navigation controller. In your storyboard, select the initial view controller in your hierarchy. With this view controller selected, choose the menu item Editor -> Embed In -> Navigation Controller .

How do I get navigation controller root view controller?

The root view controller is simply the view controller that sits at the bottom of the navigation stack. You can access the navigation controller's array of view controllers through its viewControllers property. To access the root view controller, we ask for the first item of the array of view controllers.

How do I add a view controller to my storyboard?

Go back to your . storyboard file. In the Object Library, find the View Controller object (see Figure 4-16) and drag and drop it onto the storyboard, on the right side of your existing view controller, as shown in Figure 4-17.


2 Answers

There is a side effect with just using [viewController loadView] and that is that viewDidLoad will not be called automatically after the view has loaded.

To do it properly and to make sure that viewDidLoad gets called, instead of calling loadView yourself you can instantiate the UIViewController´s view using

  UIView *view = myViewController.view;

or

  UIView *view = [myViewController view];

This will both call loadView (if the view was not already loaded) and then viewDidLoad when the view has finished loading.

like image 123
jake_hetfield Avatar answered Oct 19 '22 00:10

jake_hetfield


In response to the accepted answer, you must not call -loadView manually. From the documentation:

You should never call this method directly. The view controller calls this method when its view property is requested but is currently nil. This method loads or creates a view and assigns it to the view property.

Instead you should just call [myViewController view] which will trigger the view to load.

As always, read the documentation!

like image 32
Mike Weller Avatar answered Oct 19 '22 00:10

Mike Weller