Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How to access assign something to a navigationItem of a ViewController from another view controller?

ViewController A:

self.navigationItem.titleView = ActivityIndicator.showActivityIndicator("Updating location...")

... after some time the self.navigationItem.titleView should be set to nil from another view controller:

ViewController B: ViewControllerA.navigationItem.titleView = nil

but that doesn't work.

1 Answer

Hello Talebi,

Please use the following code inside the calling method in view controller B:

    let splitViewController = self.splitViewController
    let navigationController = splitViewController?.viewControllers.first as? UINavigationController
    let masterViewController = navigationController?.viewControllers[0] as? MasterViewController // view controller A
    masterViewController?.navigationItem.titleView = nil

I hope that this code works for you, and keep in mind that I assume you are using a master/detail application template with a split view controller.

Hi. Thanks but I'm not using a split view controller. I'm trying to access navigationItem.titleView of ViewCtrl B from ViewCtrl A (both are TableViewControllers)