Objective C UINavigationController not working as expected

UINavigationController seems to not be working. The expected behavior is to click on one of the items in the table, and have it navigate to the next page. Furthermore, I was hoping to see a navigation bar at the top of the table, but I don't.

// AppDelegate.h
#import <UIKit/UIKit.h>
#import "TableViewControllerMusicians.h"
#import "ViewController2.h"
#import "ViewController3.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UITabBarController *tabBar;

@property (strong, nonatomic) UINavigationController *nav;
@property (strong, nonatomic) TableViewControllerMusicians *tvMusicians;

@property (strong, nonatomic) ViewController2 *vc2;
@property (strong, nonatomic) ViewController3 *vc3;

Open in new window


// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] init];
    self.window.backgroundColor = [UIColor lightGrayColor];
    self.window.frame = [UIScreen mainScreen].bounds;
    
    self.tabBar = [[UITabBarController alloc] initWithNibName:nil bundle:nil];

    self.tvMusicians = [[TableViewControllerMusicians alloc] initWithStyle:UITableViewStylePlain];
    self.tvMusicians.title = @"Musicians";
    
    self.nav = [[UINavigationController alloc] initWithRootViewController:self.tvMusicians];
    
    self.vc2 = [[ViewController2 alloc] initWithNibName:nil bundle:nil];
    self.vc2.title = @"VC2";
    self.vc2.tabBarItem.title = @"My VC2";
    
    self.vc3 = [[ViewController3 alloc] initWithNibName:nil bundle:nil];
    self.vc3.title = @"VC3";
    self.vc3.tabBarItem.title = @"My VC3";
    
    
    self.tabBar.viewControllers = [NSArray arrayWithObjects: self.tvMusicians, self.vc2, self.vc3, nil];
    
    self.window.rootViewController = self.tabBar;
    
    [self.window makeKeyAndVisible];
    
    return YES;
}

Open in new window


// TableViewControllerMusicians.m
#import "TableViewControllerMusicians.h"

@interface TableViewControllerMusicians ()

@property() NSArray *musicianList;

@end

@implementation TableViewControllerMusicians

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.musicianList = [NSArray arrayWithObjects:
                         @"Imagine Dragons",
                         @"Coldplay",
                         @"John Newman",
                         @"Sam Smith",
                         nil];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.musicianList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    
    // Configure the cell...
    cell.textLabel.text = [self.musicianList objectAtIndex:indexPath.row];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *musician = [self.musicianList objectAtIndex:indexPath.row];
    
    UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    vc.title = musician;
    [self.navigationController pushViewController:vc animated:YES];
}

@end

Open in new window

LVL 8
pzozulkaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

darbid73Commented:
Hi pzozulka,

It looks like you are really coming along fast.  I would really recommend you look at the storyboard as it might make a lot of these things a lot easier.  But this is just a personal preference or subjective opinion.

From the look of your code, you have added all of your controllers to your TabBar.  You are then trying to "push" from your tabbar to somewhere else.  Also because your NavigationController is in your tabbar this is why you dont see the navigation bar.

You will need to make your navigationcontroller your root view and then add your views including a tabbar to this to then be able to push between views.

One of many tutorials on UINavigationControllers
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pzozulkaAuthor Commented:
For this project, the requirement was to delete the storyboard, as the goal is to learn the coding aspect.

Thanks for your help. With your help, I figured out what my issue was, once again :)

When I was setting up my UITabBarController with the array of objects, the first object I was adding was the TableViewController, when I should have been adding the UINavigationController. I failed to realize that the root wasn't the Table, it was the Nav.

Thanks for your help. I'll review the tutorial as well. Literally spent two hours yesterday banging my head against the wall. Thanks again for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Swift Programming

From novice to tech pro — start learning today.