pzozulka
asked on
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
// AppDelegate.m
// TableViewControllerMusicia ns.m
// 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;
// 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;
}
// TableViewControllerMusicia
#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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.