0

I have a simple Tab bar navigation app

-------------------
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|       (o)       |
|                 |
[ Tab 1 ]_[ Tab 2 ]

When Tab 1 is touched, Screen 1 is shown. When Tab 2 is pressed, Screen 2 is shown.

How can I call an action each time a screen is shown (not just on the viewDidLoad event).

Do I have to listen to the touch event on the Tab bar or is there an event in the ViewController I can listen for?

bodacious
  • 6,608
  • 9
  • 45
  • 74
  • 1
    "When Tab 1 is touched, Screen 1 is shown. When Tab 2 is pressed, Screen 2 is shown?" Yes it is. – Manlio Feb 27 '12 at 15:06
  • *is there an event in the ViewController I can listen for?* What did you find when you looked at the [documentation for UIViewController](https://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIViewController_Class/Reference/Reference.html)? – Caleb Feb 27 '12 at 15:17

2 Answers2

5

viewDidLoad gets called when the nib is being loaded into memory. When a view is actually appearing on the screen, viewDidAppear gets called.

[Edit]

This is the (natural) order of callbacks: initWithNibName:Bundle: -> awakeFromNib -> loadView -> viewDidLoad -> viewWillAppear -> viewDidAppear

[Edit2]

see here for an excellent explanation: https://stackoverflow.com/a/5109277/347353

Community
  • 1
  • 1
Pooria Azimi
  • 8,585
  • 5
  • 30
  • 41
2

You can use viewWillAppear and viewDidAppear.

  1. viewWillAppear

    Notifies the view controller that its view is about to be added to a view hierarchy.

  2. viewDidAppear

    Notifies the view controller that its view was added to a view hierarchy.

sch
  • 27,436
  • 3
  • 68
  • 83