user2618815
user2618815

Reputation:

Adding a navigation controller to a tab bar application

I have two questions about this one. First, I have the navigation controller successfully put in the storyboard and is linked with the tabs and is working how I would want it to. Except for one thing. When I try to add a code such as this
[self.navigationController popToViewController:vc animated:YES] I get an error Property 'navigationController' not found on object of type 'AppDelegate *'

Is this because I put it in the wrong place? Or becasue its a tabbar application and something aint right.

Upvotes: 0

Views: 673

Answers (1)

Sam Spencer
Sam Spencer

Reputation: 8608

It sounds like you're trying to make a call to your navigation controller from your AppDelegate. Unless you've specifically setup your AppDelegate to work with your navigation controller (it'd need to be a subclass of UIViewController), you'll get an error because there is no Navigation Controller on your AppDelegate class (by default). Therefore, when you make that call - the navigation controller can't be found. Notice how your AppDelegate is a subclass of UIResponder, not UIViewController:

@interface AppDelegate : UIResponder <UIApplicationDelegate>

Instead, create and / or connect your navigation controller to a UIViewController subclass - then you can make calls like this from your subclass:

[self.navigationController popToViewController:vc animated:YES];

To create and setup a Navigation Controller, follow these steps (may vary if you aren't using storyboards).

  1. Create a new UINavigationController Obj-C subclass. In the Xcode menu bar, select File > New, or press CMD+N. Name your class and set its superclass as UINavigationController: Adding a UINavigationController Subclass
    Note that you don't absolutely need an entirely new class! You can use an existing class that is a subclass of UIViewController - as long as the navigationController property is available.
  2. Add your navigation controller from the objects library in Xcode and set it up the way you need it.
  3. Select the NavigationController in your Storyboard, then open the Utilities Panel, and select the Identity Inspector Tab. Set the Custom Class name to the name of your UIViewController or UINavigationController subclass: Navigation Controller Custom Class Xcode 4.6
  4. From your class you'll be able to use the navigationController property, among hundreds of others relating to the View Controller. Remember that the AppDelegate is really a place for setting up your app and handling app events (ex. app closing, app backgrounding, app opening).

Upvotes: 1

Related Questions