ViewController 加载后不会更新

发布于 2024-12-17 03:52:09 字数 564 浏览 0 评论 0原文

我有两个使用导航栏的视图控制器。第一个 viewController 显示我在第二个 viewController 上更改的一些数据。

因此,如果我加载第二个 viewController,导航栏中会出现一个后退按钮,我可以更改我的值(并且它们被存储,我使用了调试器)。我的问题是,点击后退按钮进入我的firstView控制器后,它没有调用它的viewDidLoad方法。很明显,当不调用此函数时,根本没有更新的值。

第一次启动时,viewDidLoad 方法被调用并执行我希望它执行的操作。在视图控制器之间来回切换后,不会再次调用该方法。

有什么解决办法吗?

谢谢!

编辑:

已解决

我不想删除我的问题,也许有人也需要这个:

每次视图出现时都会调用此方法,默认情况下可能未定义:

-(void)viewDidAppear:(BOOL)animated
{
NSLog(@"View appeared.");
}

I got two viewControllers using a navigation bar. The first viewController displays some data I change on the second viewController.

So if I load the second viewController, a back button appears in the NavBar and I can change my values (and they are stored, I used the debugger). My problem is, after hitting the backButton to come to my firstView Controller, it does not call it's viewDidLoad method. It's clear, that there are no updated values at all, when this function is not called.

At the first start, the viewDidLoad method is called and does what I want it to do. After going back and forth between the viewControllers the method is not called again.

Any solutions?

Thanks!

EDIT:

SOLVED

I did not want to delete my question, maybe someone needs this too:

This method is called every time the view appears, it is probably not defined by default:

-(void)viewDidAppear:(BOOL)animated
{
NSLog(@"View appeared.");
}

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

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

发布评论

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

评论(2

也只是曾经 2024-12-24 03:52:09

但是 viewWillAppear 中的更新代码(如 [[self view] setNeedsDisplay];)。

为了明确起见:加载视图时会调用 viewDidLoad 。这会在第一次显示视图时发生。当您导航到下一个视图时,仍然可以加载第一个视图(取决于您的代码)。因此,当您导航回该视图时,将不会调用 viewDidLoad 因为该视图仍然存在。

但每次要显示视图时(例如,当您导航回此视图时),都会调用 viewWillAppear 和 viewDidAppear 方法。

希望有帮助。

But the update code (like [[self view] setNeedsDisplay];) in viewWillAppear.

To make it clear: viewDidLoad is called when your view is loaded. This happens at the first time the view is going to be displayed. When you then navigate to the next view the first view can (depending on your code) still be loaded. Therefore when you navigate back to that view viewDidLoad won't be called because the view is still there.

But every time the view is going to be shown (for example when you navigate back to this view) the method viewWillAppear and viewDidAppear will be called.

Hope that helps.

梦初启 2024-12-24 03:52:09

当视图控制器分配初始化发生时,ViewDidLoad 方法将被调用。在导航控制器移回的情况下,它不会创建任何新的视图控制器,而是重用导航堆栈中的先前引用。

因此 viewDidLoad 将不会被调用。因为您的视图控制器已经在内存堆栈中。因此,它只会使视图重新出现在窗口上,并分别调用 viewWillAppear

ViewDidLoad method will get called when there is view controller allocation-initialization happens. In the case of moving back in navigation controller, it is not creating any new view controllers, it is reusing previous references from navigation stack.

Hence viewDidLoad will not get called. As your view controller is already in memory stack. So it will just make the view to reappear on windows and it will call viewWillAppear respectively.

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