xcode 如何在后退按钮上重新加载上一个视图控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31725878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:35:19  来源:igfitidea点击:

How to reload the previous view controller on back button?

iosobjective-ciphonexcodeipad

提问by TechChain

I have two view controller A& B.On ViewControllerAi am displaying data from server.I have nav button Update to go to ViewControllerB.On Bi can update the data which was shown on ViewControllerB.Now when data is updated on server i show an alert 'Update Success'on click of Ok i go to previous ViewController.But i don't see the updated data because i removed the ViewControllerBfrom stack but Ais not reloded.

我有两个视图控制器AB。ViewControllerA 上,我正在显示来自服务器的数据。我有导航按钮更新以转到ViewControllerB。B 上,我可以更新ViewControllerB上显示的数据。现在,当服务器上的数据更新时,我会显示单击“确定”时会出现“更新成功”警报,我转到上一个ViewController。但我没有看到更新的数据,因为我从堆栈中删除了ViewControllerB,但A没有重放。

Code for going back to ViewController A

返回到 ViewController A 的代码

  [self.navigationController popViewControllerAnimated:YES];

回答by iAnurag

I assume you are sending a web call from viewDidLoad(). So in order to update your viewController AAdd your web call method in viewWillAppear()

我假设您正在从viewDidLoad(). 所以为了更新你的viewController AviewWillAppear()

回答by Aaron

The state of your "data" on view controller A has not changed and you need to refresh it. If you're fetching data for view controller A within -viewDidLoad, try moving it to a different life cycle method like -viewWillAppear.

视图控制器 A 上的“数据”状态没有改变,您需要刷新它。如果您要为 中的视图控制器 A 获取数据-viewDidLoad,请尝试将其移动到不同的生命周期方法,例如-viewWillAppear

Remember, -viewDidLoadgets called only when the view controller loads which happens once, however -viewWillAppeargets called before it appears which will happen often if you're hopping back and forth between views.

请记住,-viewDidLoad仅在视图控制器加载时才被调用,这会发生一次,但是-viewWillAppear在它出现之前被调用,如果您在视图之间来回跳跃,这将经常发生。

回答by Ranu Dhurandhar

You have to call your API method in viewWillAppear(). So, whenever you come back from any viewController, your updated server data will reload itself.

您必须在viewWillAppear(). 因此,无论何时从任何 viewController 返回,更新后的服务器数据都会重新加载。