在 UINavigationController 中按下后退按钮时如何调用 viewDidLoad 方法? (iPhone / iPad)
我需要一种在 UINavigationController
中按下“后退”按钮时调用 viewDidLoad
方法的方法。例如,如果我有这样的流程:
查看 A ->查看B->查看C
我需要它,这样如果我在 View C
上并且按回键,则应调用 View B
的 viewDidLoad
方法。同样,如果我在 View B
上并按下后退按钮,则应调用 View A
的 viewDidLoad
方法。
请有人建议如何实现这一目标?
I need a way of calling the viewDidLoad
method when the 'back' button is pressed in the UINavigationController
. So for example if I have a flow like so:
View A -> View B -> View C
I need the it so that if I'm on View C
and I press back, View B
's viewDidLoad
method should be called. Similarly, if I'm on View B
and I press the back button View A
's viewDidLoad
method should be called.
Please can someone suggest how to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当视图控制器完成加载和设置视图控制器(例如加载 NIB 文件)时,将调用 viewDidLoad 方法。根据您的需要,您应该将相关代码移至 viewWillAppear ,每次相应的视图控制器变得可见时都会调用该代码。因此对于转换
A -> B-> C(返回)-> B
B
视图控制器的viewWillAppear
方法将被调用两次。The
viewDidLoad
method is called when the view controller has finished loading and setting up the view controller, for example loading the NIB file. For your needs, you should move the relevant code toviewWillAppear
which will be called each time the corresponding view controller becomes visible. So for a transitionA -> B -> C (backto)-> B
theviewWillAppear
method ofB
's view controller will be called twice.根据您的描述,听起来使用 viewWillAppear 或 viewDidAppear 方法会更好。 viewDidLoad 被设计为仅被调用一次,除非视图由于内存警告而被丢弃。
From your description, it sounds like you would be better served by using the viewWillAppear or viewDidAppear methods instead. viewDidLoad is designed to only be called once, unless the view is discarded because of a memory warning.