Update the UITabBar tab view or call the method from the UITabBar tab view when the tab is re-selected.

Except that the first tab switches to when ViewDidLoad is initially launched, all subsequent actions to switch to (reselect) this tab do not seem to launch ViewDidLoad or any other method. It seems that the view was cached and renamed instantly, just as it was left when the user switched from this tab. However, when a particular tab is re-selected by the user, I need to update its presentation. Thus, if possible, I need to somehow call the method from the view without additional user interaction (after re-selecting the tab).

I know that there is a method in UITabBarDelegate that must be called before or after switching the tab, but I don’t know if this will be a way to start a method call from the tab view to reactivate the specified tab. I do not know how to access the already active instance of the tab view from the delegate. Any direction on this would be very helpful!

+6
source share
2 answers

When using UITabBar, your views are kept alive unless a warning about saving memory is received and need to be freed. Since they are constantly stored, viewDidLoad will be called only once (if your view is not unloaded due to a memory warning).

viewWillAppear and viewDidAppear will be called each time this view is displayed on the screen, so you want to update the view or data.

viewWillAppear is best used in cases where you want to update something in the view before the user sees it. For example, setting up UILabels or UITextFields to clear data. This will prevent the user from seeing a quick flash of old data before clearing it. Running anything in this method that will consume significant system resources or take a lot of time (i.e. Webservices) can slow down your application and prevent you from showing UIActivityIndicator at the same time, since this is before the view really appears .

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

+13
source

I do the same in one of my applications, and I use viewWillAppear and viewDidAppear to accomplish this.

It seems that they are called every time the tab is re-selected. (I created an updateView method that is called inside the viewDidAppear method)

+3
source

Source: https://habr.com/ru/post/894940/


All Articles