Reputation: 316
I'm creating a tab bar application for the iPhone and I want whatever view is currently active to be fully unloaded/released if a new tab is selected. When any tab is reselected I want it to have to reload everything. The reason I seek this feature is because all views interact with a single database, and can modify the database. When the views are built they are built off the current database, which means that they can be out of date without a forced reload of the view.
To see what I'm referring to load the "Phone" app on your iPhone and you can type in a number on the keypad, switch tabs, switch back to the keypad and the number you typed remains there. Which is a desirable trait for the Phone app, but not so much for what I'm designing.
Is there a way to achieve this? Or, should I use another method to update my views when tabs are switched?
Upvotes: 3
Views: 2905
Reputation: 6383
I think Neil Galiaskarov comment that you should not think about releasing the view. His idea to put your current reloading logics in -(void)viewWillAppear
is sound:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppeared:animated];
// Any code you are currently triggering in your init / viewDidLoad
// to reload your database and display the result
}
This is triggered every time you return via the tab-bar and should give you the result you are after. There are few caveats however, depending on what you do next, that might not deliver the required functionality:
viewWillAppear
and viewDidAppear
).If the latter is a problem one way to handle it through a BOOL _shouldTriggerReload;
:
Wrap the viewWillAppear
logic inside an if:
if (_shouldTriggerReload){
_shouldTriggerReload = NO; // Preparing for the next round
// Any code you are currently triggering in your init / viewDidLoad
// to reload your database and display the result
}
Then in your viewDidDisappear
:
- (void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
_shouldTriggerReload = YES;
}
And it will also have to be set inside your viewDidLoad for that first entry:
- (void)viewDidLoad{
_shouldTriggerReload = YES;
}
Upvotes: 3
Reputation: 4792
tabBarController has a viewControllers
property.
You modify the viewControllers property to achieve such effect.
What i would do is,
For the view controller(say VC)
which you want to reload its view, when user selects that tab.
add UITabBarControllerDelegate
delegate to VC. and implement below method.
– tabBarController:shouldSelectViewController:
when the user selects this in bottom tab bar, tabBarController
calls this method whether should it show this viewController or not.
Here, change the tabBarController.viewControllers
property and return YES.
Lets say your tab bar has only two viewControllers attached to it, and user now selects tab 2 and you have to create new viewController and show it. So,
UITabBarController *tabBarController;
NSMutableArray *array=[NSMutableArray arrayWithArray:tabBarController.viewControllers];
UIViewController *viewController2; //init your viewController type 2
[array replaceObjectAtIndex:1 withObject:viewController2];
tabBarController.viewControllers =[NSArray arrayWithArray:array];
Upvotes: 1
Reputation: 47049
Use delegate method
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
Add your piece of code which you can trigger each time whenever you select tabBarItem.
Upvotes: 0