Reputation: 12431
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.
Upvotes: 5
Views: 14017
Reputation: 12325
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.
Upvotes: 1
Reputation: 9318
You can send NSNotification
after
pressing done button on second tab
and handle this notification on first tab controller.
Upvotes: 2
Reputation: 9157
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.
Upvotes: 7