How to show/push a navigation controller programatically - ios

Using a storyboard, I can have a controller have a show/push segue to a navigation controller. The new navigation controller then will have the navigationController set to the existing navigation controller and the view controllers of the new navigation controller will have the new navigation controller as their navigation controllers. If I try programmatically pushing a navigation controller it fails though. Is there any programatic way of modifying the view controllers of a navigation controller so that they can contain a navigation controller? The following won't work:
let aNavigationController = UIStoryboard(name: "OurStoryBoard", bundle: nil).instantiateViewControllerWithIdentifier("OurNavigationController") as OurNavigationController
navigationController!.pushViewControll(aNavigationController, animated: true)

NavigationControllers do not push each other. You can replace a NavigationController with another one, but that will not involve a "pushing" animation.
You can, however, set different view controllers on the same Navigation Controller and have an animation. These will replace the existing view controllers inside the Navigation Controller. Here's an example to push one View Controller and replace any existing view controllers inside the navigation controller:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ViewControllerOne *vc1 = [storyboard instantiateViewControllerWithIdentifier:#"ViewControllerOne"];
NSMutableArray *controllers = [NSMutableArray array];
[controllers addObject:vc1];
[yourAppNavigationController setViewControllers:controllers animated:YES];
If you want to replace the current view controller(s) with multiple view controllers:
// 1.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
FirstViewController *vc1 = [storyboard instantiateViewControllerWithIdentifier:#"FirstViewController"];
SecondViewController *vc2 = [storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
ThirdViewController *vc3 = [storyboard instantiateViewControllerWithIdentifier:#"ThirdViewController"];
// 2.
NSMutableArray *controllers = [NSMutableArray array];
[controllers addObject:vc1];
[controllers addObject:vc2];
[controllers addObject:vc3];
// 3.
[yourAppNavigationController setViewControllers:controllers animated:YES];
Again, this involves the "pushing" animation that you are probably looking for, and it replaces the existing view controllers so there won't be an undesired back button (in the first code example w/ one VC).

Related

Pass variable to View Controller Embedded in Navigation Controller in Objective-C

I have embedded a destination viewcontroller in a navigation controller and am now unable to pass it a variable.
My code is:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
//THis is the navigation controller
UIViewController *destVC = [storyboard instantiateViewControllerWithIdentifier:#"myNav"];
//This is the view controller embedded in the nav
IDNowVC* myVC = [storyboard instantiateViewControllerWithIdentifier:#"myVC"];
myVC.sender = #1;//for contacts
[self presentViewController:destVC animated:YES completion:nil];
After the launch of the VC, sender property is nil. It is not getting the value #1.
What am I missing?
Thanks for any suggestions.
In your code you have a comment that says:
//This is the view controller embedded in the nav
However, the view controller below that comment is not the one embedded in your navigation controller. It's a completely new controller that is created at that line and disposed of at the end of the function.
You need something more like this:
UINavigationController *destVC = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:#"myNav"];
IDNowVC* myVC = destVC.childViewControllers[0];
myVC.sender = #1;
There might be some syntax issues with the above...

How to pass value to a viewController embedded in NavigationController

There are similar questions but they are either in swift or are not solving my problem.I've a view controller which is presenting navigation view controller when cell did select button is pressed as:
patientBillNavigationViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PatientBillVC"];
//soem value assignment
[self presentViewController:viewController animated:YES completion:nil];
You can say billing opens up a whole new view independent of the main app flow to handle billing process.
The view this navigation View Controller automatically loads is the bill view and now if I want to pass a value from this viewcontroller to the other viewController embedded in navigation view I can't do that. How to pass a value?
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
PatientBillViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"PBVC"];
vc.superBillToAddEdit = sb;
//then display the view embedded in navigation view
But this is not working.
One way I know is to Subclass UINavigationViewController and in its viewDidLoad, you can do:
YourEmbeddedVc *svc =self.viewControllers[0]; //get the controller reference
svc.name= #"Hello";
I'm assuming you are only having one controller in your navigation stack, if not you need to find out the proper match of the required VC.
I might not be understanding the question exactly, correct me if I'm wrong.
It sounds like you want to start a nav controller from a view controller (let's call this firstViewController) and pass a value from firstViewController to the view controller that will eventually load in the nav controller.
If that's the case, why don't you create the second view controller (billingStep1ViewController) and then assign the value to it as a property, then pass the billingStep1ViewController to the nav controller's initWithRootViewController: method?
Something like this (untested code, btw):
// this code would go inside our FirstViewController file
// create the first vc that will be loaded in the nav controller
BillingStep1ViewController *billingStepOneVc = [self.storyboard instantiateViewControllerWithIdentifier:#"stepOne"];
// set the value you want
billingStepOneVc.bill = myBill;
// create new nav controller with step one vc as the root
UINavigationController *uiNavController = [[UINavigationController alloc] initWithRootViewController:billingStepOneVc];
[self presentViewController:uiNavController];
That way you can have two view controllers talking directly with one another and not worry about the navigation controller at all.
Update: Here is some tested code that works to illustrate my idea:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
//create the step 1 view controller - this is the first view controller we will see in the navigation controller
StepOneViewController *stepOneVc = [storyboard instantiateViewControllerWithIdentifier:#"stepOne"];
//assign a value to it (name is an NSString #property of StepOneViewController)
stepOneVc.name = #"Jones";
//this is the nav controller we will display, we set the root vc to our step one.
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:stepOneVc];
//present the nav controller on screen.
[self presentViewController:navController animated:YES completion:nil];
Some of the class names changed but the idea is the same.
Rather than creating the navigation controller from its storyboard identifier, create it programmatically, with the PatientBillViewController you created as its root view controller. Like this:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
PatientBillViewController *pbVC = [mainStoryboard instantiateViewControllerWithIdentifier:#"PBVC"];
pbVC.superBillToAddEdit = sb;
PatientBillNavigationViewController * navVC = [[PatientBillNavigationViewController alloc] initWithRootViewController:pbVC];
[self presentViewController:navVC animated:true completion:nil];
Swift Version
Here is the swift version answer for the above problem!!
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PatientBillVC") as! patientBillNavigationViewController
vc.name = "name"
let mVc = UINavigationController.init(rootViewController: vc)
self.navigationController?.present(mVc, animated: true, completion: nil)

Move to navigation controller from swrevealcontroller?

I have used SWRevealController in my app.On click of item in UITableView I want to go to a navigation controller so that I can switch between multiple view controllers.But as of now I am not able to redirect to navigation controller.I have used below code but it just replace the controller.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
MoreHelpController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"HelpController"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[navController setViewControllers: #[rootViewController] animated: YES];
[self.revealViewController setFrontViewController:navController];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
Please tell how can I directly go to navigation controller.It is very confusing here.
You just cannot move to navigation controller. Navigation Controller takes you to another view controller from one view controller.
If you want to move to another view controller on selection of table view row, then use didSelectRowAtIndexPath method of tableview and inside that use this:
yourViewController *obj = [self.storyboard instantiateViewControllerWithIdentifier:#"yourViewControllerIdentifier"];
[self.navigationController pushViewController:obj animated:YES];

properly set up navigation controller as a root for a uitab bar

I am a little bit confused and cannot make things to work.
So, this is my code in my app Delegate.
// If the device is an iPad, we make it taller.
_tabBarController = [[AKTabBarController alloc] initWithTabBarHeight:(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? 70 : 50];
NSString * storyboardName = #"Main_iPhone";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
CurrentViewController * tab1VC = [storyboard instantiateViewControllerWithIdentifier:#"Tab1"];
// dummy controllers
CurrentViewController * tab2VC = [storyboard instantiateViewControllerWithIdentifier:#"Tab1"];
CurrentViewController * tab3VC = [storyboard instantiateViewControllerWithIdentifier:#"Tab1"];
CurrentViewController * tab4VC = [storyboard instantiateViewControllerWithIdentifier:#"Tab1"];
[_tabBarController setViewControllers:[NSMutableArray arrayWithObjects:
tab1VC,
tab2VC,
tab3VC,
tab4VC,
nil]];
[_window setRootViewController:_tabBarController];
[_window makeKeyAndVisible];
Then, in my storyboard I have the image below :
So, I have my ViewController and I clicked Embed In > Navigation Controller
I want to have a different navigation controller for each tab.
Inside my CurrentViewController I have this when button is clicked :
- (IBAction)dummyButton:(id)sender {
NSString * storyboardName = #"Main_iPhone";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UserSettingsSetupViewController *userSettingsSetupController = [storyboard instantiateViewControllerWithIdentifier:#"UserSettingsSetup"];
[[self navigationController] pushViewController:userSettingsSetupController animated:YES];
}
And apparently nothing is pushed into the navigation Controller because nothing opens.
Doing some research, I saw that this is because [self navigationController] or self.navigationController returns nil.
but why is that? Should I add any code? I thought that by doing that from storyboard, it shouldn't be nil? Am I wrong?
A couple of comments. First of all, if you're using a storyboard, don't create view controllers in the app delegate; drag all the controllers you need into the storyboard. There's no need for any added code in the app delegate at all.
Your problem is caused by trying to mix storyboard controllers and code in the app delegate. You are instantiating tab1VC and making that the controller in the first tab -- that controller doesn't "know" anything about the navigation controller you added in the storyboard. If you wanted to do it in code (which I don't recommend), you would need to instantiate that navigation controller instead (it will take care of instantiating its root view controller), and add that as the first item in the viewControllers array.
My advice is do it all in the storyboard. Change the class of the tab bar controller there to AKTabBarController, and set its tab bar height in its init method or viewDidLoad.

Push UIViewController to UINavigationController from a popover

I have a problem when trying to push a new view controller onto an existing navigation controller.
The thing I'm trying is to make a UIPopoverController appear when pushing a navigation UIBarButtonItem, and from that "dropdown" select a menu point which will push the associated view controller onto the "main" navigation controller.
I've tried the following, which gives a modal. But I want the view pushed.
If selecting push instead of modal the result is as following.
I've also tried making a custom UITableViewController (on the popover) from which I've tried the following code:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *dash = [storyboard instantiateViewControllerWithIdentifier:#"dash_nav"];
UIViewController *students = [storyboard instantiateViewControllerWithIdentifier:#"students"];
if (indexPath.row == 0) {
[dash pushViewController:students animated:YES];
// [[dash navigationController] presentViewController:students animated:YES completion:nil];
}
NSLog(#"%#", [dash title]);
NSLog(#"index = %i", indexPath.row);
}
Is there a way to do what I am trying to accomplish?
This code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *dash = [storyboard instantiateViewControllerWithIdentifier:#"dash_nav"];
UIViewController *students = [storyboard instantiateViewControllerWithIdentifier:#"students"];
is creating too many new instances. You should be using the existing storyboard (self. storyboard) and the existing navigation controller. The navigation controller needs to be passed to the table view controller (which you should use because the storyboard doesn't have the required information). We'll call this originatingNavigationController, a new #property on the table view controller.
When the segue triggers to show the popover, set the navigation controller reference into the destination view controller (the table view).
Then, in the didSelectRowAtIndexPath: method you just instantiate the students VC and push it:
UIViewController *students = [self.storyboard instantiateViewControllerWithIdentifier:#"students"];
[self.originatingNavigationController pushViewController:students animated:YES];
and then the table view controller should dismiss itself (its popover).

Resources