5

I have 2 tabbars in my tabbar controller. I am currently in the second tabbar, after I click on a 'done' button, the tabbar controller needs to switch to the first tab and auto refresh the tableview in it.

I am able to execute on the first part

//Switch to the first tab's view
self.tabBarController.selectedViewController 
              = [self.tabBarController.viewControllers objectAtIndex:0];

However I need some advise on how to refresh the first view's tableview immediately after the switch is made. Any advise is appreciated.

EDIT: Updated requirement

Note: I do not want the view in the tabbar to reload everytime someone clicks on the tab. The reload should only happen in tab 1's tableview when

1) User is in tab 2

2) User clicks on 'Done button' in tab 2

3) App switches to tab 1 + Reloads the tableview in tab 1

Rationale is that the user will update some information in tab 2 which can only be viewed in tab 1. I want the user to be able to view the updated info immediately after switching to tab 1.

Zhen
  • 12,361
  • 38
  • 122
  • 199
  • possible duplicate of [Reload UITableView viewWillAppear](http://stackoverflow.com/questions/3896239/reload-uitableview-viewwillappear) and [Tabbar reload every time when tab is pressed](http://stackoverflow.com/questions/5946511/tabbar-reload-every-time-when-preseed-tab) – jscs Jun 18 '11 at 20:09
  • Hi Josh, I believe that my situation is a little different. I do not want the view to reload everytime I click on the tab but only when a certain action is triggered i.e. a button in a different tab is pressed etc – Zhen Jun 18 '11 at 23:53

3 Answers3

7

You could just do [tableView reloadData] in the first view's viewDidAppear: method?

EDIT: Editing to add additional data.

General caveat here, Apple tends to frown upon having custom UI Patterns like this. I'm not sure what your app is trying to do here, but you should mostly stick with switching tabs only if the user explicitly clicks on that tab. Have you looked at modal view controllers if you want to present a screen and get some data back, instead of having it in a tab bar?

If not, I guess you could have a solution where the first tab is a delegate of the second tab. When the button is clicked, you could call [delegate doneBtnClicked:] so the control shifts to the first tab. In that function, you could use the setSelectedIndex: to your current tab and call reloadData: on the table view.

Tejaswi Yerukalapudi
  • 8,987
  • 12
  • 60
  • 101
  • Hi, I actually do not want the tableview to reload EVERYTIME it appears. I only want it to reload after I click on the done button in the second tab. – Zhen Jun 18 '11 at 19:42
  • Hi, I believe that is what Instagram is currently doing with their flow i.e. take a photo in photo tab, apply filter, click done, switch to Feed tab where they will see the photo they have just uploaded. I wanted to model a behaviour like that as well – Zhen Jun 18 '11 at 23:57
2

You can send NSNotification after

pressing done button on second tab

and handle this notification on first tab controller.

jamapag
  • 9,290
  • 4
  • 34
  • 28
1

So, create a DONE Button as a UIBarButtonItem, and in it's IBAction, do the following.

For reloading the tableView for the view in the first tab, put that tableView as that class' property , and reference the class like @class class1 , and create an object as a property class1 *object1 for View1 (Tab1) in the class for View2 (Tab2), and access the array1 using object1 like

[class1.object1.array1 reloadData];

And for bringing the view to tab one, use

self.tabBarController.selectedIndex = 0;

Thats pretty much it !

Alternatively you can also use a singleton class for this purpose.

Legolas
  • 12,145
  • 12
  • 79
  • 132