how do i add a navigationViewController to a UIViewController? - iphone

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.

Related

UITabBarController Issue

I want to create something like below.
RootView does not have TabBar, From the second view there should be TabBar.
What I have currently done is, I am using UINavigationController as controller calass
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *rootController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
But how can I use UITabBar with tabBarController from SecondViewController?
create the objects of the second view and then push your view with the tabbarcontroller
From Storyboard embed your SecondViewController in TabBar. Select your controller and go to Editor -> Embed in -> TabBar Controller! I'm from my mobile..sorry if i have any mispells!
You need to push your Tabbar controller's object. Initialize your tab bar controller's object and add all other controller objects to the tabbar controller's viewcontroller array.
On button action:-
1> Initialize tab bar controller and suppose you name its object as objTab;
2> objTab.viewcontrollers = [NSArray arrayWithObjects:..] ---> Objects of all viewcontrollers that are a part of your tab bar controller. Thus all objects need to be created first.
3> self.navigationcontroller pushViewController: objTAb
Something like this should do the trick (not using ARC):
//vc1, vc2, vc3 = your view controllers
NSArray *viewControllersArray = [NSArray arrayWithObjects:vc1,vc2,vc3, nil];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:viewControllersArray];
[self.navigationController pushViewController:tabBarController animated:YES];
[tabBarController release];
What you want to do is create the UITabBarController and push that along the navigation stack.
For Sample Write loadnewview method at appdelegate .Use buttonPressed method for button action or any object action of first view controller as shown below to display tab bar from second view controller.
I have taken two tabs for sample so I wrote Capacity as 2. You can take up to 5.
-(IBAction)buttonPressed:(id)sender
{
HomeViewController *homeVC=[[HomeViewController alloc]initWithNibName:#"HomeViewController" bundle:nil];
[self.navigationController pushViewController:homeVC animated:YES];
[appDelegate loadnewview];
}
-(void)loadnewview
{
if(!self.tabBarController)
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate=self;
NSMutableArray *localcontrollerarray = [[NSMutableArray alloc] initWithCapacity:2];
UIViewController *viewController1 = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
UINavigationController *navi1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
[localcontrollerarray addObject:navi1];
UIViewController *viewController2 = [[ScanViewController alloc] initWithNibName:#"ScanViewController" bundle:nil];
UINavigationController *navi2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
[localcontrollerarray addObject:navi2];
self.tabBarController.viewControllers = localcontrollerarray;
[self.window addSubview:self.tabBarController.view];
}
use this type of method in AppDelegate.m and property-synthesize UITabBarController and store array of viewcontroller in it also in application didFinishLaunchingWithOptions method just assign navigationViewController as a RootViewController like bellow..
RootViewController *masterViewController = [[[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];
after then when you want to add TabBar to any view at that time call this bellow method like this..
[appDelegate addTabBarControllerInwindow];
-(void)addTabBarControllerInwindow
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.window cache:YES];
[self.navigationController.view removeFromSuperview];
[self.window addSubview:[tabBarController view]];//tabBarController.view
[UIView commitAnimations];
}
Create an Application using Tab Bar Controller and on the ViewDidLoad method of the view controller for which you want to hide the tab bar use code:
[self.tabBarController.tabBar setHidden:YES];
And don't forget to unhide the tab bar using the same code replacing NO instead of YES for the view controller for which you want to show the tab bar.

How to convert my view based application to navigation based application?

Can anybody explain me the step by step process of converting a view based application to navigation based application?which are the steps taken in both IB and in My appdelegate to achieve this?
In your app delegate you can put your code like this:
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:navigationController.view];
add a UInavigation controller object in the interface file.and then add the code in your didfinishlaunch
self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.navigationController setNavigationBarHidden:NO];
//self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
//self.navigationController.navigationBar.translucent = YES;
// Override point for customization after app launch
[window addSubview:self.navigationController.view];
[window makeKeyAndVisible];
return YES;
and dont forget to connect the delegate to filesowner in IB.

how to add navigationcontroller to uiviewcontroller

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

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 push UIViewController

I have UIViewController in Viewbased application.
I want to push another UIViewcontroller from it.
-(IBAction) Myfunction
{
MedicineSearchSystem *medicineSearchSystem = [[MedicineSearchSystem alloc] initWithNibName:#"MedicineSearchSystem" bundle:nil];
[self.parentViewController:medicineSearchSystem animated:YES]; // Crash here
}
As warrenm already told you, first check if your viewController has a navigationController by calling something like : NSLog(#"%#", self.navigationController) then you can push using:
[self.navigationController pushViewController:medicineSearchSystem animated:YES];
self.mainItemListViewController = [[[NCItemsViewController alloc] init] autorelease];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.mainItemListViewController];
[window addSubview:[self.navigationController view]];
[window makeKeyAndVisible];