返回导航控制器层次结构时如何触发 viewWillAppear?

发布于 2024-10-03 07:08:35 字数 774 浏览 6 评论 0原文

当返回到 UINavigationController 的第一级时,我一直在尝试各种方法来触发一些代码,但是以下方法都不起作用...

// HomeViewController.m
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    NSLog(@"trigger 1");
}

- (void)viewWillAppear:(BOOL)animated {
    NSLog(@"trigger 2");
}

-(void) viewDidAppear:(BOOL)animated {
    NSLog(@"trigger 3");
}

我开始认为我的应用程序设置错误了..是不是有什么问题UIViewControllers 中有 UIViewControllers 吗?

这是应用程序的笔尖。它是一个 UITabBarController,每个选项卡都有一个导航控制器,其中包含 UINavigationController 的第一页。此设置是方法未触发的原因吗?

alt text

这里未显示底部的 UIView,它链接到文件所有者的视图。更复杂的是,MainViewController 实际上是实用程序风格应用程序的另一面。

I've been trying various methods to trigger some code when returning to the first level of a UINavigationController, but none of the below work...

// HomeViewController.m
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    NSLog(@"trigger 1");
}

- (void)viewWillAppear:(BOOL)animated {
    NSLog(@"trigger 2");
}

-(void) viewDidAppear:(BOOL)animated {
    NSLog(@"trigger 3");
}

I'm starting to think I've set up my app wrong.. is it something about having UIViewControllers within UIViewControllers?

This is the nib for the app.. it's a UITabBarController with a navigation controller for each tab, and within that the first page of the UINavigationController. Is this setup the reason why the methods are not triggering?

alt text

Not shown in this is a UIView at the bottom which is linked to the File Owner's view. And to make it more complicated, MainViewController is actually the flip side of a Utility-style app.

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

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

发布评论

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

评论(1

夜访吸血鬼 2024-10-10 07:08:35

只是说我用另一种方式解决了这个问题。我需要调用像 viewWillAppear 这样的东西,因为当第二个视图中的某些内容发生更改时,我需要更新我的第一个视图表。

我实现它的方式是,在我的“tableView:commitEditingStyle”方法(在删除某些内容后调用)中,我将其放在最后。

[[[[self.navigationController.tabBarController.viewControllers objectAtIndex:0] viewControllers] objectAtIndex:0] reloadTheTable];

reloadTheTable 是我的 HomeViewController 中的一个方法,它执行更新数组和 [tableView reloadData] 等操作;

这是很多括号。我确信我的笔尖可以设置得更简单。我认为卡斯帕罗夫说过他认为前面有四步……而四个括号是我的极限。

我通过做这样的事情来解决这个问题:

NSString *name = NSStringFromClass ([[self parentViewController] class]);
NSLog(@"%@", name); // outputs UINavigationController

并慢慢添加它,直到找到我正在寻找的东西:

name = NSStringFromClass ([[[[self.navigationController.tabBarController.viewControllers objectAtIndex:0] viewControllers] objectAtIndex:0] class]); //UINavigationController
NSLog(@"%@", name); // outputs HomeViewController

当您将视图放入视图中时,viewWillAppear 被取消的方式肯定有些奇怪。

Just to say I solved this another way. I needed to call something like viewWillAppear because I needed to update my first view table when something had changed in my second view.

The way I achieved it was, in my 'tableView: commitEditingStyle' method (called after I delete something), I put this at the end..

[[[[self.navigationController.tabBarController.viewControllers objectAtIndex:0] viewControllers] objectAtIndex:0] reloadTheTable];

reloadTheTable is a method in my HomeViewController which does stuff like update arrays and [tableView reloadData] etc;

That's a lot of brackets. I'm sure my nib can be set up to make that simpler. I think Kasparov has said that he thinks four moves ahead... and four brackets is my limit.

I worked it out by doing things like this:

NSString *name = NSStringFromClass ([[self parentViewController] class]);
NSLog(@"%@", name); // outputs UINavigationController

and slowly adding to it until I found what I was looking for:

name = NSStringFromClass ([[[[self.navigationController.tabBarController.viewControllers objectAtIndex:0] viewControllers] objectAtIndex:0] class]); //UINavigationController
NSLog(@"%@", name); // outputs HomeViewController

There is something definitely odd in the way viewWillAppear gets cancelled when you puts views within views.

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