手动调用 viewDidAppear 的正确时间?

发布于 2024-08-05 00:51:22 字数 403 浏览 4 评论 0原文

我的应用程序中有一个 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 技术交流群。

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

发布评论

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

评论(4

じее 2024-08-12 00:51:22

如果您使用视图控制器包含,请勿直接调用 viewWillAppear:。而是使用 – beginAppearanceTransition:animated:– endAppearanceTransition

如果您正在实现自定义容器控制器,请使用此方法告诉子级其视图即将出现或消失。不要直接调用 viewWillAppear:、viewWillDisappear:、viewDidAppear: 或 viewDidDisappear:。

如果视图的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.

If you are implementing a custom container controller, use this method to tell the child that its views are about to appear or disappear. Do not invoke viewWillAppear:, viewWillDisappear:, viewDidAppear:, or viewDidDisappear: directly.

Calling addSubView will automatically trigger viewWillAppear: and viewDidAppear: if the view's viewController is a child view controller, therefore calling viewWillAppear: 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.

往昔成烟 2024-08-12 00:51:22

在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.

反话 2024-08-12 00:51:22

从父控制器的 -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)

浅黛梨妆こ 2024-08-12 00:51:22

这就是我手动调用 viewWillAppearviewDidAppearviewWillDisappearviewDidDisappear 的方式:此处

,我以这种方式加载的视图控制器之一具有以下 viewWillAppear: (请注意,这是 viewWillAppear 因为在调用 viewDidAppear 时,用户可以查看您的视图)

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [wordListTable reloadData];
    [wordListTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}

This is how I manually call viewWillAppear, viewDidAppear, viewWillDisappear, viewDidDisappear: here

and one of the view controllers I load this way, has the following viewWillAppear: (note that this is viewWillAppear since at the point that viewDidAppear is called, your view is viewable by the user)

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [wordListTable reloadData];
    [wordListTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文