change RootviewController to Navigation controller - iphone

I am new to iPhone,
I want to change my Rootviewcontroller to my new class and make it to navigation controller.
Here is my code snippet,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:detailViewController];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
I am getting SIGABRT says 'adding a root view controller <NewClass: 0x6a8dd50> as a child of view controller:

Whenever u want to set:
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:detailViewController];
self.window.rootViewController =nil;
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
EDIT : Directly use AppDelegate instance to set rootViewController for UIWindow as i have shown above.

Instead of:
[self.window addSubview:navigationController.view];
make navigationController the rootViewController of window:
self.window.rootViewController = navigationController;
Also, is detailViewController of type UINavigationController? You cannot set UINavigationController as root to another UINavigationController object.

Just add this line,
RootViewController *defaultViewController=[[RootViewController alloc]initWithNibName:#"NAME_OF_XIB" bundle:nil];
before UINavigationController's initialization,
RootViewController *defaultViewController=[[RootViewController alloc]initWithNibName:#"NAME_OF_XIB" bundle:nil];
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:detailViewController];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;

Change your RootViewController to NavigationController..
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:self.detailViewController];

Related

How to add navigation controller in landscape ios6

While am trying to add navigation controller in landscape based app ios6 application it shows in Portrait Mode only like in the image,What change should i made?
In app delegate file coding,
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:[navigationController view]];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
Check this to add UINavigationController in your app :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// start of your application:didFinishLaunchingWithOptions
// ...
viewController = [[ViewController alloc]init];
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:viewController];
[self.window setRootViewController:navigationController];
[window makeKeyAndVisible];
[viewController release];
[navigationController release];
return YES;
}

self.navigationController is null

in my viewcontroller,I have a button,when press the button,entry the navigationController,my code like:
-(IBAction)ShangHaiButtonPressed:(id)sender{
marketviewcontroller = [[MarketViewController alloc]initWithNibName:#"MarketViewController" bundle:nil];
NSLog(#"%#",self.navigationController);
[self.navigationController pushViewController:marketviewcontroller animated:YES];
[marketviewcontroller release];
}
but I can see the self.navigationController is null,how to solve this problem?thank you.
update:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_switchviewcontroller = [[SwitchViewController alloc]initWithNibName:#"SwitchViewController" bundle:nil];
[self.window addSubview:_switchviewcontroller.view];
[self.window makeKeyAndVisible];
return YES;
}
The navigationController property of a view controller will return a valid navigation controller object only if the view controller is in a navigation controller's navigation stack. A view controller can be added to a navigation stack in the following ways.
By making the view controller the rootViewController of a navigation controller using initWithRootViewController: method of UINavigationController
By pushing the view controller using pushViewController: method of UINavigationController.
Make sure your view controller is added to the navigation stack in any of the above ways.
EDIT: (After the didFinishLaunchingWithOptions: code added to the question):
Change the didFinishLaunchingWithOptions: method to this,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_switchviewcontroller = [[SwitchViewController alloc]initWithNibName:#"SwitchViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:_switchviewcontroller];
[self.window addSubview:navController.view];
[navController release];
[self.window makeKeyAndVisible];
return YES;
}
Swift 4 (version):
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
switchviewcontroller = SwitchViewController(nibName: "SwitchViewController", bundle: nil)
let navController = UINavigationController(rootViewController: switchviewcontroller)
window.addSubview(navController.view)
window.makeKeyAndVisible()
return true
}
This code will yield the solution you're looking for:
-(IBAction)ShangHaiButtonPressed:(id)sender {
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self];
[self.view removeFromSuperview];
[appDelegate.window addSubview:nav.view]; // appDelegate is the delegate of your Application
marketViewController = [[MarketViewController alloc] initWithNibName:#"MarketViewController" bundle:nil];
[nav pushViewController:marketViewController animated:YES];
[marketViewController release];
}
In AppDelegate.m file make your first view RootView for Navigation :
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
myView *Obj=[[myView alloc]initWithNibName:#"myView" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:Obj];
nav.navigationBar.barStyle = UIBarStyleBlackOpaque;
[window addSubview:nav.view];
[self.window makeKeyAndVisible];
return YES;
}
In your myView.m file add below code to navigate to myNewView from myView :
-(void) registerMethod {
myNewView *obj = [[myView alloc] initWithNibName:#"myNewView" bundle:nil];
[self.navigationController pushViewController:obj animated:YES];
[obj release];
}

Implement navigation controller's in a view controller

I have a view controller and I want to implement a navigation controller inside it, but when I implemented it through Interface Builder or programmatically it doesn't work.
Most of it I implemented through Interface Builder, but here is the code I implemented in the AppDelegate, and which I am trying to implement in my view controller.
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[window addSubview:navController.view];
I know I can't implement "window" in a view controller so I tried this in my view controller:
[self.view addSubview:navController];
But it still doesn't work.
In the app delegate's applicationDidFinishLaunching you have to add this code.
viewController=[[myViewController alloc]init];
navigationController=[[UINavigationController alloc]initWithRootViewController:viewController];
[self.window addSubview:navigationController.view];
where viewController is the object of the myViewController class to which u need to add the navigation controller. It s declared in the header file of appDelegate.Similarly the navigationController is also declared in tat. Hope this helps.
New code:
AppSettings *settings = [[AppSettings alloc] init];
UINavigationController *navCont = [[UINavigationController alloc] initWithRootViewController:settings];
navCont.navigationBar.barStyle = UIBarStyleBlackTranslucent;
[self.navigationController presentModalViewController:navCont animated:YES];
[settings release];
[navCont release];
Try this in your -applicationDidFinishLaunching:
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.window setRootViewController:navigationController];
[self.window makeKeyAndVisible];

How to Use TabBarViewController with a NavigationController

I know I can do this
[self.navigationController pushViewController:self.someUITabBarController animated:YES];
And that means putting UITabBarController on a navigationgController somehow
What about if I want someUITabBarController to be the first controller (the one located on the lowest level) of navigationController?
I simply cannot change the rootViewController of the NavigationController into someUITabBarController
Erm not sure this is what you want. Below this code will be put under the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions in you "appDelegate" class.
UITabBarController *tabController = [[UITabBarController alloc] init];
UIViewController *viewController1 = ...
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController1];
NSArray *controllers = [NSArray arrayWithObjects:navigationController, nil]; // can add more if you want
[tabController setViewControllers:controllers];
// this is for custom title and image in the tabBar item
navigationController.tabBarItem.title = #"abc";
[navigationController.tabBarItem setImage:[UIImage imageNamed:#"abc.png"]];
self.window.rootViewController = tabController; // or [self.window addSubview: tabController.view];
[self.window makeKeyAndVisible];
I am not sure if this works. But try this,
UINavigationController *navCont = [[UINavigationController alloc] init];
[navCont pushViewController:navCont animated:NO];

how do i add a navigationViewController to a UIViewController?

how do i add a navigationViewController to a UIViewController?
- (void)applicationDidFinishLaunching:(UIApplication *)application {
loginViewController *vc1=[[loginViewController alloc]initWithNibName:#"login" bundle:[NSBundle mainBundle]];
rootViewController* vc2 = [[[rootViewController alloc] init] autorelease];
UINavigationController* navController = [[[UINavigationController alloc]
initWithRootViewController:vc2] autorelease];
NSArray* controllers = [NSArray arrayWithObjects:vc1,navController, nil];
//loginViewController.viewControllers = controllers;
[window addSubview:[self.loginController view]];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
i'm stuck with this.need some help...
The simplest way to do this is to change your init when setting up the navController. Also, you'll want to keep the navController around, probably as a member variable of your app delegate:
//in your header file:
....class definition
UINavigationController *_navigationController;
....
//in your implementation file:
_navigationController = [[UINavigationController alloc] initWithRootViewController: rootViewController];
//optional: if you want to start off 'one level in' to your navigation stack:
[_navigationController pushViewController: vc1 animated: NO];
[window addSubview _navigationController.view];
if I understand right, you should use
[window addSubview:[self.navController view]];
then you can send push/pop messages to self.navigationController to manage content of navigationController's stack.