是否可以手动调用viewDidAppear? [英] Correct time to call viewDidAppear manually?

查看:238
本文介绍了是否可以手动调用viewDidAppear?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有一个 UITableViewController ,它直接添加到视图层次结构中。视图出现后,我想滚动到特定的单元格。我的解决方案是调用代码滚动 - [viewDidAppear]

I have a UITableViewController in my app, which is added to the view hierarchy directly. After the view appears, I want to scroll to a specific cell. My solution would be to call the code for scrolling in -[viewDidAppear].

根据Apple的文档,我有手动调用该方法:

According to Apple's docs I have to call the method manually:


如果属于视图控制器的视图直接添加到视图层次结构中,视图控制器将不会收到这条信息。如果您向视图层次结构插入或添加视图,并且它具有视图控制器,则应直接向关联的视图控制器发送此消息。

If the view belonging to a view controller is added to a view hierarchy directly, the view controller will not receive this message. If you insert or add a view to the view hierarchy, and it has a view controller, you should send the associated view controller this message directly.

问题是:什么时候是手动调用它的时候?

The question is: When is the right time to call it manually?

当我尝试滚动时,从父视图控制器的 - [viewDidAppear] 调用它会导致崩溃因为显然,桌面视图实际上还没有出现,因此认为它没有要滚动的部分。

Calling it from the parent view controller's -[viewDidAppear] leads to a crash when I try to do the scrolling because apparently, the table view actually didn't yet appear and therefore thinks it has no sections to scroll to.

推荐答案

从父控制器的 -viewDidAppear 调用它通常是你最好的选择。

Calling it from the parent controller's -viewDidAppear is usually your best bet.

如果子视图控制器尚未完全初始化,如果这会导致问题,则可能还有其他问题。在调用 -viewWillAppear 之后,确保您的子视图控制器完全准备好行动(您也可以从父级的 -viewWillAppear 手动调用)

If this results in problems if the child view controller isn't fully initialized yet, you may have another problem. Make sure your child view controller is completely "ready for action" after -viewWillAppear is called (which you can also call manually from the parent's -viewWillAppear)

这篇关于是否可以手动调用viewDidAppear?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆