手动调用 viewDidAppear 的正确时间?
我的应用程序中有一个 UITableViewController ,它直接添加到视图层次结构中。视图出现后,我想滚动到特定的单元格。我的解决方案是在 -[viewDidAppear]
中调用滚动代码。
根据苹果的文档,我必须手动调用该方法:
如果属于视图控制器的视图直接添加到视图层次结构中,则视图控制器将不会收到此消息。如果您向视图层次结构中插入或添加视图,并且该视图具有视图控制器,则应直接向关联的视图控制器发送此消息。
问题是:什么时候才是手动调用的合适时机?
当我尝试滚动时,从父视图控制器的 -[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]
.
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用视图控制器包含,请勿直接调用
viewWillAppear:
。而是使用– beginAppearanceTransition:animated:
和– endAppearanceTransition
。如果视图的viewController是子视图控制器,调用addSubView会自动触发
viewWillAppear:
和viewDidAppear:
,因此直接调用viewWillAppear:
会触发view will出现方法两次。使用 beginAppearanceTransition:animated:and
– endAppearanceTransition` 将抑制自动行为,因此您只会调用它一次。If you are using view controller containment don't call
viewWillAppear:
directly. Instead use– beginAppearanceTransition:animated:
and– endAppearanceTransition
.Calling addSubView will automatically trigger
viewWillAppear:
andviewDidAppear:
if the view's viewController is a child view controller, therefore callingviewWillAppear:
directly will trigger view will appearance method twice. Using beginAppearanceTransition:animated:and
– endAppearanceTransition` will suppress the automatic behaviour so you only get it called once.在tableview上的-[viewDidAppear]中,确实从父视图控制器的-[viewDidAppear]调用,您可以调用[tableView reloadData],这样您就可以确保tableView已加载并准备就绪。
In -[viewDidAppear] on the tableview, called indeed from the parent view controller's -[viewDidAppear], you can call [tableView reloadData], this way you ensure that the tableView is loaded and ready.
从父控制器的 -viewDidAppear 调用它通常是最好的选择。
如果子视图控制器尚未完全初始化,这会导致问题,那么您可能会遇到另一个问题。确保您的子视图控制器在调用 -viewWillAppear 后完全“准备好执行操作”(您也可以从父级的 -viewWillAppear 手动调用)
Calling it from the parent controller's -viewDidAppear is usually your best bet.
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)
这就是我手动调用
viewWillAppear
、viewDidAppear
、viewWillDisappear
、viewDidDisappear
的方式:此处,我以这种方式加载的视图控制器之一具有以下
viewWillAppear:
(请注意,这是viewWillAppear
因为在调用viewDidAppear
时,用户可以查看您的视图)This is how I manually call
viewWillAppear
,viewDidAppear
,viewWillDisappear
,viewDidDisappear
: hereand one of the view controllers I load this way, has the following
viewWillAppear:
(note that this isviewWillAppear
since at the point thatviewDidAppear
is called, your view is viewable by the user)