如何Nav点击后退按钮时刷新之前一个UITableView

来源:互联网 发布:数据采集仪的使用方法 编辑:程序博客网 时间:2024/04/28 12:42

  

Just so we're clear: view A is the starting point. User taps something and you slide right to view B. User taps the back button and you're going from B back to A and you want to do something as a result of the 'back' action.

There are three ways to do it (and on neither do you have to go near the navigationController -- these apply to the underlying viewControllers themselves):

  • As dmercredi suggests override viewWillAppear on view controller A so when you're heading back to it, it refreshes itself. Problem is that viewWillAppear is also called when A is called the very first time. So you'll have to set some sort of flag to distinguish between the firstviewWillAppear and any subsequent ones when returning from B.

  • Override viewWillDisappear on view controller B and do your refreshing there. This will only get called when B is about to go away. If there's something on B that goes one level deeper or brings up a modal dialog on top, viewWillDisappear is going to get called so again you'll have to distinguish between the coming and the going.


  • Decouple the various views and use the delegate pattern. View controller A sets itself as a delegate of B and when B updates something it invokes the delegate method, so A is notified of the change and can update whatever it needs to. You can invoke the delegate method any time the user makes a change inside B or override viewWillDisappear and just do it one time on the way out. 

    老外总结的三种方法,还有个方法,也是个老外说得,把View A的指针直接传给B,然后让B直接去调用这个指针做更新,不太确定是否可行,感觉理论上没什么问题。实践中是用方法三完成的,View controller A将其自身设成B的delegate,当B需要更新A的时候,就调用A的delegate方法,同时在A的delegate方法中视图更新动作[tableView reloadData]. 其中tableView是一个IBOutlet,指向View Controller A中的TableView.网上好多例子都是用的viewWillAppear,但是有人说会有明显刷新的痕迹,就是屏幕会闪一下。目前先用方法三对付,如果后期发现更好的方法再改改。
原创粉丝点击