How do I add a navigationbar to my app programmatically? - iphone

I set up my app a while ago using a tutorial for setting up the navigationbar in interface builder, but no longer use interface builder in any of my app and would much like to change this 1 thing which does use interface builder to being coded in. So my question is, I have a navigationbar which works, and which appears on the first view of my app, HomeView. How would I make this happen just as it does now, programmatically?

In the AppDelegate.m file, add this:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
RootViewController *rootViewController = [[RootViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:rootViewController];
[window addSubview:[navController view]];
[self.window makeKeyAndVisible];
}
Be sure to add #import "RootViewController.h" at the top of the file.

Another way of adding the navigation bar programmatically, change the application:didFinishLaunchingWithOptions method of your app delegate like:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
RootViewController *rootViewController = [[RootViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:rootViewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
}

Related

NavigationController not loading view

I'm using xCode 4.3.2 and started a blank application.
I have a navigation controller and a simple logincontroller. I want the login controller to be my root view so it is this first thing that a user does when they login.
I'm using the following code and when I run the application it displays a black screen. I put in logging in the LoginViewController.m->ViewDidLoad and it is being run is there something im doing wrong the LoginViewController.xib is very simple it just contains a button right now that will switch to a tab view controller once I figure this out.
Thanks in advance.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
UIViewController *loginController = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:loginController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window addSubview:navigationController.view];
[self.window makeKeyWindow];
return YES;
}
This is not right:
[self.window addSubview:navigationController.view];
change it to this:
self.window.rootViewController = navigationController;

Convert View Based application to NavigationBased

I am new to iPhone development.
I created simple ViewBased Application in which, i have 2 pages Licpage and PlanPage
i have set LicPage as my RootViewController in didFinishLaunchingWithOptions,
Now when i click on button in LicPage i will navigated to PlanPage but in my PlanPage
i am unable to see my NavigationBar with back button on it.
Note: I can't drag NavigationBar manually with back button on it. because when i will add 3rd page, it will also navigate to 2nd page(PlanPage). and when i will click on back button it will take me on firstpage(LicPage) not in third page.
Try this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[LicAppViewController alloc] initWithNibName:#"LicAppViewController" bundle:nil] autorelease];
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
You need to make NavigationController in appDelegate class (delete viewController rootViewController).
And use this in .h
#property (nonatomic, retain) UINavigationController *navigationController;
in .m
#synthesize navigationController
and make object of LicPage (objLicPage) and set as rootviewcontroller for navigation controller
self.navigationController.rootViewController = objLicPage;
[self.window addSubView:navigationController.view];
[self.window makeKeyAndVisible];

iphone simulator message "My Universal App on Iphone" was does it mean?

I run the simulator on a very simple app- a navigation controller contained in a view controller. The application is a view based app with no modification done to the header, and the below code in the implementation.
I get this message "My Universal App on Iphone" superimposed over the subview of the navigation controller and I literally saw one result on google that mentioned it! How do I get rid of this message?
#import "[header filename]"
#implementation tutorial_navigationcontrollerAppDelegate
#synthesize window=_window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
//ViewController *viewController = [[ViewController alloc] init];
//viewController.title = #"Hello Nav";
//UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
UIViewController *x = [[UIViewController alloc] init];
x.title = #"XXX";
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:x];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {}
- (void)applicationDidEnterBackground:(UIApplication *)application {}
- (void)applicationWillEnterForeground:(UIApplication *)application {}
- (void)applicationDidBecomeActive:(UIApplication *)application {}
- (void)applicationWillTerminate:(UIApplication *)application {}
- (void)dealloc
{
[_window release];
[super dealloc];
}
#end
I noticed the same thing while adding a view controller. While I can't explain the label doesn't disappear like it should, I did notice that if you change the controller's view in any way it will remove the text.
UIViewController *x = [[UIViewController alloc] init];
x.view.backgroundColor = [UIColor whiteColor];
Edit: My previous answer did work to a degree, but it turns out it's actually pretty straightforward. Check out MainWindow_iPhone.xib and MainWIndow_iPad.xib. Just delete the labels sitting in the view!

iphone: need to implement navigationController inside viewcontroller which appears after selecting a tab in tabbar

I am pretty new to UITabBarController. I was trying to provide a navigation system in a viewController corresponding to a tab in tabViewController
created an instance of navigation controller in viewDidLOad
[testLabel setText:#"Test"];
self.navigator=[[UINavigationController alloc] initWithRootViewController:self];
[super viewDidLoad];
on button click I do this
NSLog(#"I am here");
StartWordPickerVC *aStartWordPickerVC=[[StartWordPickerVC alloc] initWithNibName:#"StartWordPickerVC" bundle:nil];
[self.navigator pushViewController:aStartWordPickerVC animated:YES];
[aStartWordPickerVC release];
But when I click button nothing happens
Can you please help me out in this
Thanks
Just add this in AppDelegate.h
UINavigationController *navigationController;
Just add this in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
}

iPhone - App always starts with an empty white screen?

I can't get my iPhone app to display any of the content on startup.
Any idea what's wrong here?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
HomeViewController *t = [[HomeViewController alloc] init];
[self.window addSubview:t.view];
[self.window makeKeyAndVisible];
// I tried the following in case the problem is my
//view controller but nothing shows up at all
UISegmentedControl *t = [[UISegmentedControl alloc] init];
[self.window addSubview:t];
[self.window makeKeyAndVisible];
}
Unless your view controller implements loadView, you should load the view from a NIB file with initWithNibName:bundle:.
A segmented control should be initialized with initWithItems:. You should also assign a frame to the control.