iOS 开发:如何在将视图控制器推送到导航堆栈之前预加载它?

发布于 2024-10-08 01:13:36 字数 461 浏览 7 评论 0原文

我正在深入研究 iOS 开发,并正在构建一款益智游戏来帮助我熟悉该平台。我让 AdWhirl 在用户完成拼图时显示的分数屏幕中显示广告。问题是,乐谱屏幕视图控制器至少需要几秒钟来请求和接收广告,在此期间用户查看乐谱并移至不同的视图。我计划实现一个在用户解决谜题时发生的动画,并且动画完成所需的时间是请求和接收将在用户下一个(得分)视图中显示的广告的好时机也会被采取。

在解谜动画发生期间,如何预加载下一个视图控制器,以便在将视图控制器推送到导航堆栈时显示广告?如果这是不可能的,或者这是一个坏主意,您对我如何在动画播放时请求和接收广告有什么建议吗?

预先非常感谢您的智慧!

注意:对于那些不熟悉 AdWhirl 的人来说,请求和接收广告的过程很简单。在要显示广告的视图控制器的 viewDidLoad 方法中,创建一个 AdWhirlView(UIView 的子类),将其添加为子视图,然后调用请求广告的方法。当广告到达时,它会调用委托方法将其显示在父视图中。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

十年九夏 2024-10-15 01:13:36

仅使用 [viewController loadView] 有一个副作用,那就是 viewDidLoad 在视图加载后不会自动调用。

为了正确执行此操作并确保调用 viewDidLoad,您可以使用

  UIView *view = myViewController.view;

  UIView *view = [myViewController view];

实例化 UIViewController 的视图,而不是自己调用loadView 这将调用 loadView (如果视图尚未加载),然后在视图完成时调用 viewDidLoad加载中。

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.

他是夢罘是命 2024-10-15 01:13:36

为了响应已接受的答案,您不得手动调用 -loadView。来自 文档

您不应该直接调用此方法。视图控制器调用
当请求其视图属性但当前为 nil 时使用此方法。
该方法加载或创建视图并将其分配给视图
财产。

相反,您应该只调用 [myViewController view] 这将触发视图加载。

一如既往,阅读文档!

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!

眼泪淡了忧伤 2024-10-15 01:13:36

在 iOS 9 中添加了一个新方法 loadViewIfNeeded() 。我相信它最初是用于视图控制器的单元测试,但它也应该适用于这种情况。

In iOS 9 a new method was added loadViewIfNeeded(). I believe it was originally intended for unit testing of view controllers, but it should work for this case as well.

枯寂 2024-10-15 01:13:36

将其放在委托的 application:didFinishLaunchingWithOptions:

  dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
                   UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MyStoryboard" bundle:[NSBundle mainBundle]];
                   [storyboard instantiateViewControllerWithIdentifier: @"TheViewControllerYouWantToPreload"];
                 });

dispatch_async 将使整个事情在后台加载。

Put this on your delegate's application:didFinishLaunchingWithOptions:

  dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
                   UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MyStoryboard" bundle:[NSBundle mainBundle]];
                   [storyboard instantiateViewControllerWithIdentifier: @"TheViewControllerYouWantToPreload"];
                 });

The dispatch_async will make the whole thing load in background.

听风吹 2024-10-15 01:13:36

iOS 5 及以上版本提供了两个功能。

  • setNeedsLayout()
  • layoutIfNeeded()

调用setNeedsLayout()就像告诉系统你想要更新被调用的视图和子视图的布局并重绘它们。更改不会立即反映出来。正如名称所示,它只是设置标记并将其反映在调度中。仅当返回下一个更新周期时,更改才能反映在视图中。

重要的是,调用该方法引起的任务是异步执行的。因此,开发人员不知道更新周期何时会反映更改。

另一方面,layoutIfNeeded() 是同步调用。他们要求系统立即反映更改。顾名思义,布局是立即完成的。因此,调用此方法后返回控制时,更改已反映在视图中。

Two functions are available in iOS 5 and above.

  • setNeedsLayout()
  • layoutIfNeeded()

Calling setNeedsLayout() is like telling the system that you want to update the layout of the called views and sub-views and redraw them. Changes are not immediately reflected. As the name also includes, it simply sets the mark and reflects it in the scheduling. Changes can only be reflected in the view when the next update cycle is returned.

It is important that the task caused by the call of this method is performed asynchronously. Therefore, the developer does not know when the update cycle will come to reflect the change.

On the other hand, layoutIfNeeded() is a synchronous call. They are asking the system to reflect the changes immediately. As the name suggests, layout is done immediately. Thus, the change is already reflected in the view by the time control is returned after the call of this method has been made.

魔法唧唧 2024-10-15 01:13:36

你可以手动调用loadView,我不知道是否有任何副作用,但我不这么认为。

感谢 Tommy,确实,访问 view 属性可确保仅在视图层次结构尚未加载时才有效调用 loadView,从而避免在 loadView 代码确实处理它时出现任何潜在的泄漏。

You can call the loadView manually, I don't know if there are any side effect but I don't think so.

Thanks Tommy, indeed, accessing the view property ensure that loadView will only be effectively called only if the view hierarchy is not loaded yet, avoiding any potentiial leak if the loadView code does note cope with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文