如何“预加载”查看控制器而不实际推动它?
背景: 我有一个支持广告的免费应用程序。基本思想是,当应用程序启动时,会在后台从服务器下载广告内容(HTML),只有下载成功,才会显示内容。
以前的解决方案: 我已经在通用应用程序中成功实现了这一点。最初,我加载了一个带有 URL 的 NSData
变量,如果成功,那么我会呈现一个包含 UIWebView
的模式视图(相关代码位于 另一个问题)。问题是外部资源(图像)没有加载到原始的 NSData 请求中,因此我灵机一动想到了另一种方法:
新解决方案: 新方法(这就是我想要的帮助)是我实例化模态视图控制器(尚未显示它),并让 Web 视图开始直接加载 URL。当调用 webViewDidFinishLoad
方法时,我向父级发送一个通知,然后父级执行 presentModalViewController
。
以下是实例化视图控制器并以模态方式呈现它的方法:
- (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];
}
在 AdView 控制器中:
- (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];
}
现在上面的内容可以工作。但是,我花了很长时间才弄清楚如何在调用 presentModalViewController
之前触发 AdView 控制器中的视图控制器委托方法。 。我通过添加 [[adView view] awakeFromNib];
来做到这一点,但我知道这不是实现此目的的正确方法。
因此,上面的一切确实工作得很好,在显示视图控制器及其 UIWebView
之前有效地“预加载”它。我只是想知道我应该做什么而不是 [[adView view] awakeFromNib];
——我想以正确的方式去做。
或者说这是对的吗?我想有些人会说“如果它有效,那就是对的”,但我知道这不是真的。
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" before 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论