10

In my tab-bar I have four viewcontrollers, and what happens in one can affect the view of the other, so I may need to reload some elements in the viewcontroller when it becomes visible. Normally I'd fix that by implementing viewWillAppear, but when I switch between the tabs, viewWillAppear does not seem to get called. How can I fix that, or what should I do instead?

Update: as a PS I should add that this is a tabbarcontroller in a navigationcontroller hierarchy

Cheers

Nik

niklassaers
  • 8,480
  • 20
  • 99
  • 146
  • I'm not sure it is same problem... but might worth to loot http://stackoverflow.com/questions/131062/iphone-viewwillappear-not-firing/8157562#8157562 – Sean Nov 16 '11 at 19:40

4 Answers4

10

You may use the tabbar controller delegate works like a charm

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    [viewController viewWillAppear:YES];

}
Radix
  • 3,639
  • 3
  • 31
  • 47
1

And in case you find this question because you would like to update something in the UITabBarController itself, not the UIViewControllers of a UITabBarController, like the OP's question. For example, hiding or displaying a custom UITabBarButton. In Swift 3.0 overriding setNeedsStatusBarAppearanceUpdate of my UITabBarController class worked for me.

override func setNeedsStatusBarAppearanceUpdate() {

}
Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
1

Please see my answer here

iPhone viewWillAppear not firing

Community
  • 1
  • 1
Sam
  • 3,659
  • 3
  • 36
  • 49
-1

viewWillAppear should only be used when the view appears, and not for updating it.

Use setNeedsDisplay on the viewController's view instead.

Neil Masson
  • 2,609
  • 1
  • 15
  • 23
tadejsv
  • 2,085
  • 1
  • 18
  • 20
  • Where would I call setNeedsDisplay from? The problem is that I need a hook for when the user chooses a new viewcontroller from the tabbar – niklassaers Jul 17 '10 at 15:58
  • 1
    Oh, sory, I mixed up things here a little bit. I'm not sure why the viewWillAppear is not getting called, but I know a way around it: Make some object a tabBarController delegate, and it will the send the message, that a new view was selected, and call view will appear from there. – tadejsv Jul 17 '10 at 18:23
  • Sounds like an interesting idea, I'll give that a try tomorrow :-) – niklassaers Jul 17 '10 at 19:35