ViewController 加载后不会更新
我有两个使用导航栏的视图控制器。第一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
但是
viewWillAppear
中的更新代码(如[[self view] setNeedsDisplay];
)。为了明确起见:加载视图时会调用
viewDidLoad
。这会在第一次显示视图时发生。当您导航到下一个视图时,仍然可以加载第一个视图(取决于您的代码)。因此,当您导航回该视图时,将不会调用viewDidLoad
因为该视图仍然存在。但每次要显示视图时(例如,当您导航回此视图时),都会调用 viewWillAppear 和 viewDidAppear 方法。
希望有帮助。
But the update code (like
[[self view] setNeedsDisplay];
) inviewWillAppear
.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 viewviewDidLoad
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
andviewDidAppear
will be called.Hope that helps.
当视图控制器分配初始化发生时,
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 callviewWillAppear
respectively.