Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh a UITabBar tab's view or call a method from the UITabBar tab's view when the tab is reselected

Except for the first time a tab is switched-to, when the ViewDidLoad is initially triggered, all subsequent actions of switching to (reselecting) that tab doesn't seem to trigger ViewDidLoad, nor any other method. The view just seemed to be cached and reshown instantly, just the way it was left when the user switched away from that tab. However, when a particular tab gets reselected by the user, I need to refresh its view. Thus, if possible, I need to somehow trigger a method from within the view without additional user interaction (after the tab reselection).

I do know there's a method in the UITabBarDelegate that is supposed to get called before or after tab switching occurs, but I wouldn't know if that would be the way to go to trigger a method call from within the tab's view upon reactivation of said tab. I don't know how I'd access the already active instance of the tab's view from within the delegate. Any direction on this would be greatly helpful!

like image 623
purefusion Avatar asked Aug 12 '11 19:08

purefusion


2 Answers

When using UITabBar, your views are kept alive unless a memory warning is received and they have to be deallocated. Because they are kept perpetually alive, viewDidLoad will only be called once (unless your view is unloaded due to a memory warning).

viewWillAppear and viewDidAppear will be called each time that view will/did show on the screen, so this is where you'd want to refresh your view or data.

viewWillAppear is better to use in instances that you want to update anything on the view before it is seen by the user. For instance, setting UILabels or UITextFields to clear data. This would prevent the user from seeing a quick flash of the old data before it is cleared out. Running anything in this method that will use significant system resources or take a long time (ie. webservices) may slow your app and will prevent you from showing the UIActivityIndicator simultaneously, as this is before the view actually appears.

viewDidAppear is useful for anything that can be updated after the view is already shown, such as webservices from the previous example.

like image 94
superjessi Avatar answered Nov 15 '22 06:11

superjessi


I do the same in one of my app's and I use the viewWillAppear and viewDidAppear to accomplish this.

They seem to be called every time the tab is re-selected. (I've created a method updateView that gets called inside the viewDidAppear method)

like image 42
Faizan S. Avatar answered Nov 15 '22 05:11

Faizan S.