Question

I've recently started learning Obj-C and according to my friend (who is experienced with Obj-C), it's better if I learnt how to create a view programmatically and not rely on StoryBoards.

So... I've been trying to create a NavigationBar with a left button on it. But, when I run it, I get the actual NavigationBar, but not the button. Here's my code:

The AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]
               initWithFrame:[[UIScreen mainScreen] bounds]];


firstController = [[MainViewController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:firstController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}

and here's the MainViewController.m:

    - (void)viewDidLoad
{
[super viewDidLoad];

UIColor *navbarColor = [UIColor whiteColor];

self.view.backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBar.barTintColor = navbarColor;


UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithTitle:@"Settings" style:UIBarButtonItemStylePlain target:self action:NULL];

self.navigationController.navigationItem.leftBarButtonItem = button;

}

Any help would be appreciated. I'm fairly new to Objective-C but I understand the code concept, so you don't need to "baby" talk it when explaining ^.^

Was it helpful?

Solution

add this line before [self.window makeKeyAndVisible];

[self.window addSubview:navController.view];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top