How to "preload" view controller without actually pushing it?
Background: I've got a free app that is ad-supported. The basic idea is that when the app is launched, ad content (HTML) is downloaded from the server in the background, and only if the download was successful, then the content is being shown.
Previous solution:
I have implemented this successfully in a universal app. Originally, I loaded a NSData
variable with an URL, and if it was successful, then I presented a modal view which contained an UIWebView
(relevant code is in another question). The problem is that the external assets (images) don't get loaded in the original NSData
request, so in a stroke of genius I thought of another way:
New solution:
The new way, and this is what I want help with, is that I'm instantiating the modal view controller (without displaying it yet), and having the web view begin loading the URL directly. When the webViewDidFinishLoad
method is called, I fire a notification back to the parent which then performs the presentModalViewController
.
Here are the methods which instantiate the view controller and present it modally:
- (void)launchAd
{
if ([ServerCheck serverReachable:@"openx.freewave-wifi.com" hideAlert:YES])
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentAdModal) name:@"AdLoaded" object:nil];
AdViewController *adView = [[AdViewController alloc] initWithNibName:nil bundle:nil];
[[adView view] awakeFromNib]; //THIS LINE IS WHAT THIS QUESTION IS ABOUT
navController = [[UINavigationController alloc] initWithRootViewController:adView];
[adView release], adView = nil;
[navController setModalPresentationStyle:UIModalPresentationFormSheet];
[navController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[[navController navigationBar] setTintColor:[UIColor colorWithRed:0.94 green:0.00 blue:0.32 alpha:1.00]];
}
else
LogError(@"Not presenting ad.");
}
- (void)presentAdModal
{
LogInfo(@"Presenting advertisement modal.");
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"AdLoaded" object:nil];
[tabBarController presentModalViewController:navController animated:YES];
[navController release];
}
And in the AdView controller:
- (void)webViewDidFinishLoad:(UIWebView *)theWebView
{
LogInfo(@"Ad content did load; we can show it.");
[timeout invalidate], timeout = nil;
[self setTitle:@"From Our Sponsor"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"AdLoaded" object:nil];
}
Now what's above does work. However, it took a long time for me to figure out how to cause the view controller delegate methods in the AdView
controller to fire before calling presentModalViewController
. I did this by adding [[adView view] awakeFromNib];
, but I know this isn't the right way to make this happen.
So, everything above does work beautifully, effectively having the view controller and its UIWebView
"preload" b开发者_如何学Pythonefore displaying it. I just want to know what I should be doing instead of [[adView view] awakeFromNib];
-- I want to do it the right way.
Or is this right? I guess some folks would say "If it works, then it's right", but I know this isn't true.
精彩评论