how to add navigationcontroller to uiviewcontroller - iphone

employeeDetailed = [[[EmployeeDetailedViewController alloc] initWithNibName:#"EmployeeDetailedViewController" bundle:nil] autorelease];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:employeeDetailed] autorelease];
[employeeDetailed release];
[self.navigationController pushViewController:navController animated:YES];
I try this its saying bad access.[crash]
how to reslove this issue.
# thanks in advance

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Set the view controller as the window's root view controller and display.
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:self.viewController] autorelease];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
employeeDetailed = [[[EmployeeDetailedViewController alloc] initWithNibName:#"EmployeeDetailedViewController" bundle:nil] autorelease];
[self. navigationController presentModalViewController: navController];
This will work for you try this.

You have autorelease set in the first line (alloc/init)
You are then explicitly releasing the view controller on line three.
You are therefore over-releasing this object and causing the crash.
You can remove the [employeeDetailed release] line and it will be fine.

You cannot add UINavigationController to existed navigation stack. Instead of you need to show new navigation controller modal like this:
employeeDetailed = [[[EmployeeDetailedViewController alloc] initWithNibName:#"EmployeeDetailedViewController" bundle:nil] autorelease];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:employeeDetailed] autorelease];
[self presentModalViewController: navController];

The Best Way I found to present Navigation Controller in a specific part in your Application is like these:
MyViewController *myViewController = [[MyViewController alloc]initwithnibname :#"MyViewController"];
UINavigationController *myNavC = [[UINavigationController alloc]initWithRootViewController:myViewController];
Then in your myViewController.m
use
[self.NavigationController pushViewController: XController animated:YES];

Related

UITabBarController disappears when pushViewController

I have an app with a UITabBar and a NavigationController. When I use pushViewController the new ViewController appears with the NavigationController and the back button, but the UITabBarController disappears. I know there are lots of questions here about the same, but any of them have solved my question, maybe because I dont understand the answers given.
Any suggestion?
ActivityViewController *activityController = [[ActivityViewController alloc] initWithNibName:#"ActivityViewController" bundle:nil];
[self.navigationController pushViewController:activityController animated:NO];
That's probably because Your rootViewController (for your main UIWindow) is set to a Navigationcontroller instead of your TabBar.
If you don't want the Tabbar to go away just set it as your root view controller
Do the following in appDidFinishLaunching in your AppDelegate
LoginViewController *loginViewController = [[FirstViewController alloc] init];
UINavigationController *loginNavigationController = [[UINavigationController alloc] loginViewController];
[firstViewController release];
self.window.rootViewController = loginNavigationController;
Then in your Login Page:
- (void)loginSuccessfull
{
FirstViewController *firstViewController = [[FirstViewController alloc] init];
UINavigationController *firstNavigationController = [[UINavigationController alloc] initWithViewController:firstViewController];
[firstViewController release];
SecondViewController *secondViewController = [[SecondViewController alloc] init];
UINavigationController *secondNavigationController = [[UINavigationController alloc] initWithViewController:secondViewController];
[secondViewController release];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:
[NSArray arrayWithObjects:firstNavigationController, secondNavigationController, nil]];
[firstNavigationController release];
[secondNavigationController release];
[self.navigationController pushViewController:tabBarController];
[tabBarController release];
}
If you still need the navigation functionality just wrap your viewControllers inside a UINavigationController, and add the serounding navigationController to the tabBar, instead of the UIViewcontroller

how to add a NavigationController to a UIViewController?

I need to add a UINavigationController to a UIViewController,and don't use appDelegate.In my Controller I write like this
OneViewController *oneVC = [[OneViewControlelr alloc] initWithNibName:nil bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:oneVC];
[self presendModalViewController:nav];
and it nothing to found.
It should be
[self presentModalViewController: nav animated:YES];
For more info, please visit UIViewController documentation
Try this :
OneViewController *oneVC = [[OneViewControlelr alloc] initWithNibName:#"OneViewControlelr" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:oneVC];
[self presentModalViewController: nav animated:YES];
if you dnt want navigationcontroller just try this
OneViewController *oneVC = [[OneViewControlelr alloc] initWithNibName:#"xibname" bundle:nil];
[self presendModalViewController:oneVC animated:YES];
[oneVC release];
if u want navigationController try this code
OneViewController *oneVC = [[OneViewControlelr alloc] initWithNibName:#"xibname" bundle:nil];
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:oneVC];
[self presendModalViewController:navigationController animated:YES];
[oneVC release];
[navigationController release];
Just do like this in AppDelegate.m file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
SoapWebServiceViewController *root = [[SoapWebServiceViewController alloc] initWithNibName:nil bundle:nil];
nav = [[UINavigationController alloc] initWithRootViewController:root];
// Add the view controller's view to the window and display.
[self.window addSubview:nav.view];
}
Hope this help U
OneViewController *oneVC = [[OneViewControlelr alloc] initWithNibName:nil bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:oneVC];
AppDelegate *appDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
[[[appDelegate <viewController>]view] removeFromSuperview]; // put UIViewController reference into <viewController>
[[appDelegate window]addSubview:[[appDelegate nav]view]];

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];

navigate from one page to another in xcode

How can I navigate from one page to another page in xcode? remember without using the interface builder... I want the answer programmatically?
Pls be more precise if you want to get what you want.
If you are using view controllers within navigationcontroller you can make use of its pushViewController: or presentModalViewController:. Or if you just want to show another view you can just add the next view to existing view as subview.
Although your question is not much clear but still I would like to give a try...
You can use
UIViewController *yourViewController = [[YourViewControllerClass alloc] initWithNibName:#"<name of xib>" bundle:nil];
[self presentModalViewController:yourViewController animated:YES];
[yourViewController release];
In case the new view is also to be created programmatically, you can do that in the viewDidLoad method of YourViewControllerClass and change the initialization to
UIViewController *yourViewController = [[YourViewControllerClass alloc] init];
In YourViewController when you wish to come back to previous view on some button action you can use
[self dismissModalViewControllerAnimated:YES];
Another way that you can do is
UIViewController *yourViewController = [[YourViewControllerClass alloc] init];
[self addSubview:[yourViewController view]];
and to remove the view you can use
[self.view removeFromSuperview];
Hope this works for you, if yes please communicate....:)
//Appdelegate.m
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = navigation;
[self.window makeKeyAndVisible];
return YES;
}
//In viewcontroller1.M
- (IBAction)GoToNext:(id)sender
{
ViewController2 *vc2 = [[ViewController2 alloc] init];
[self.navigationController pushViewController:vc2 animated:YES];
}

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.