0

If a viewController is in a navigationStack(probably top of stack),
does it automatically possess self.navigationItem?

I keep seeing self.navigationItem and self.navigationController without much context in questions and tutorials, and wonder where that variables are set.

  • edit

Wow thanks for the fast answers.
Where would I have spotted the info(about self.navigationItem) in the official doc?

eugene
  • 39,839
  • 68
  • 255
  • 489

3 Answers3

1

Yes if its in a stack it does have a self.navigationItem, if its not in a navigation stack self.navigationItem would equal to nil

This property is defined in an objective c class extension

@interface UIViewController (UINavigationControllerItem)

it defines the properties of UIViewController related to the navigation controller

Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
0

Yes, you will see the self.navigationItem and self.navigationController properties set in every controller that is added to the stack of navigation controller. These properties get set once you call pushViewController:animated or initWithRootViewController:.

Eimantas
  • 48,927
  • 17
  • 132
  • 168
0

They are not set by you, unless you specifically tell it. There are others properties that are not assigned by you directly as well, for example:

self.tabBarController;

self.parentViewController;

That's why you should check if it is nil, before trying to access a property.

Rui Peres
  • 25,741
  • 9
  • 87
  • 137