Search code examples
iosiphoneios7uinavigationcontroller

UINavigationController not appearing?


I have a Add View where when you tap on Category, the following code is executed

- (void)categoryTapped {
    CategoryGroupViewController *categoryGroupViewController = [[CategoryGroupViewController alloc] initWithNibName:@"CategoryGroupViewController" bundle:nil];
    [self presentViewController:categoryGroupViewController animated:YES completion:nil];
}

CategoryGroupViewController.h looks like

@interface CategoryGroupViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UINavigationController *navigationController;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@end

CategoryGroupViewController.m looks like

#import "CategoryGroupViewController.h"
#import "Helper.h"

static NSString *CellIdentifier = @"Cell";

@interface CategoryGroupViewController ()
@property(nonatomic, strong) NSArray *categories;
@end

@implementation CategoryGroupViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // (todo) should come from API
        self.categories = @[@"Food & Drink", @"Utilities"];
    }
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"categoryGroup View loaded");
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:self];
    self.navigationController.title = @"Pick Category";
}
...
}

When I run my application, I see the following in log

2014-11-20 21:29:53.589 myapp-ios[30332:70b] categoryGroup View loaded

But on Simulator, I see
enter image description here

Why don't I see NavigationController?


Solution

  • As you are presenting your CategoryGroupViewController it will not show Navigation Bar by default.

    You have to set your CategoryGroupViewController as rootViewController for UINavigationController and instead of presenting CategoryGroupViewController present newly created UINavigationController.

    - (void)categoryTapped{
        CategoryGroupViewController *categoryGroupVC = [[CategoryGroupViewController alloc] initWithNibName:@"CategoryGroupViewController"  bundle:nil];      
       UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController: categoryGroupVC]; 
       [self presentViewController:categoryGroupViewController animated:YES completion:nil];
    }