SWRevealViewController - push view controller from side menu - ios

I've set up my app using the SWRevealViewController and it's working fine but I want to change how it works. At the moment I have the following views on the same level:
Home
View A
View B
But I want:
Home
View A
View B
I still want Home, View A, View B to be in the menu but when clicking on View A or View B it gets pushed onto Home. And in the navigation bar it has a back button instead of the menu button.
Is this possible using SWRevealViewController?

in objective-C
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
SWRevealViewController *main = (SWRevealViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"SWRevealViewController"];
[self presentViewController:main animated:YES completion:nil];
in swift -- it automatically open the your root view controller of SWL
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let main = storyboard.instantiateViewControllerWithIdentifier("SWRevealViewController") as! UIViewController //SWRevealViewController
self.presentViewController(main, animated: true, completion: nil)

I found an answer and hopefully it helps someone reading this. It has both ways, SideMenu when you have a tabBarController or only a NavigationController. Posting snippet from answer, which worked for me.
With TabBar Controller:
UITabBarController *tbc = (UITabBarController *)self.revealViewController.frontViewController;
UINavigationController *nc = tbc.selectedViewController;
[nc pushViewController:myVC animated:NO];
[self.revealViewController setFrontViewPosition:FrontViewPositionLeft animated:YES];
With Navigation Controller:
UINavigationController *frontVC = (UINavigationController *)self.revealViewController.frontViewController;
[frontVC pushViewController: yourVC animated:NO];
[self.revealViewController pushFrontViewController:frontVC animated:YES];

Related

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)

How to passing data when using presentViewController with UITabbarController

I am a newbie in ios. I tried working with UITabbarController and I had some problem with it when I tried passing data from a ViewController (this ViewController is not an item in Tabbar) to an other ViewController that is item in Tabbar. In this case i wanted when I click button Login,"Welcome to shop" will appear and the username in Login form will pass into label in "Welcome to shop".
I use code below to make "welcom to shop" controller appearing:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UITabBarController *tabbarController= [storyboard instantiateViewControllerWithIdentifier: #"tabbarID"];
[self presentViewController: tabbarController animated: YES completion: nil];
So any idea for me to passing username (in LoginController) into label (WelcomeToShopController) ?
My problem resolved after i researched :). Because i used tabbar and navigationController so i should instance tabbar -> NavigationController -> ViewController. In my case, this code below will work perfectly.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UITabBarController *tabbarController= [storyboard instantiateViewControllerWithIdentifier: #"tabbarID"];
UINavigationController *nav = (UINavigationController *)[tabbarController.viewControllers firstObject];
HistoryOrderingController *historyOderingVC = (HistoryOrderingController *)[[nav viewControllers] firstObject];
historyOderingVC.user = self.user;
[self presentViewController: tabbarController animated: YES completion: nil];

after call presentViewController tab bar is missing

test1 is the first view in the tarbarcontorller..
after I call presentViewController tab bar is missing
How can i solve it??
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
TestTableViewController *TestTableViewController = [storyboard instantiateViewControllerWithIdentifier:#"test1"];
TestTableViewController.memberid = memberid;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:TestTableViewController];
[self.navigationController presentViewController:navigationController animated:YES completion:nil];
You should present your TabBarController, and not the first view of your tab bar controller. It will display the first view in your tab bar controller properly.

PushViewController in fullscreen

I'd like to know if it's possible to push a ViewController of the NavigationController in full screen without tabbar and navigation bar. My problem is as follow:
My app has a NavigationController embedded in a TabBarController. In the NavController I show a FirstVC with a button to navigate to another VC. Right now I do a modal presentation to show the SecondVC. But in this SecondVC I have a tableview where a button returns a ThirdVC. This ThirdVC needs to have the navigationbar and tabbar of the FirstVC. Maybe I need to show the SecondVC with a push on the NavigationController of the first but it seems that I can't reproduce my full screen animation ...
To make it simple:
FirstVC ===> SecondVC (modal presentation) ====> ThirdVC (modal presentation):
This is my app at this time without navbar or tabbar on the ThirdVC.
Thanks in advance.
Edit
The modal presentation to the SecondVC is like that:
- (IBAction)detailButtonClicked:(id)sender {
SecondVC *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondVC"];
vc.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:vc animated:YES completion:nil];
}
From the SecondVC to the ThirdVC I use again presentViewController because I can't use pushViewController in modal. The result is predictable: I don't have a navigation bar or a tabbar.
I tried different things like the one below or addChildView as subview to my FirstVC:
- (IBAction)detailButtonClicked:(id)sender {
SecondVC *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondVC"];
UINavigationController *childNavigationController = [[UINavigationController alloc] initWithRootViewController:vc];
childNavigationController.navigationBarHidden = YES;
[self.navigationController presentViewController:childNavigationController animated:YES completion:nil];
}
and in the SecondVC
- (IBAction)changeButtonClicked:(id)sender {
ThirdVC *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"ThirdVC"];
[self.navigationController pushViewController:vc animated:YES];
}
I can't access my tabbar or navigation bar in the ThirdVC either...
What I do in your is I added following code improvement.
- (IBAction)buttonClicked:(id)sender {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main"
bundle: nil];
SecondViewController *vc = (SecondViewController*)[mainStoryboard
instantiateViewControllerWithIdentifier: #"SecondVC"];
vc.delegate = self;
[[APP_DELEGATE window] addSubview:vc.view];
[self addChildViewController:vc];
}
Main line of code is addchildViewController.
Here You can download the sample code of yours.

self.navigationController is 'null' even after embed in a navigationcontroller

I want to add a navigationcontroller to an existing viewcontroller which is created using storyboard, i have embed it in a navigation controller, but the code for navigating (shown below) is not working even after embed in the navigation controller:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
EditProfileViewController *nextViewController = [storyboard instantiateViewControllerWithIdentifier:#"EPVController"];
[self.navigationController pushViewController:nextViewController animated:YES];
When i have tried to log self.navigationController, it shows null.
Update: It is fine when i am trying with presentViewcontroller , but i
want to push the viewcontroller with navigationController.
I am struggling with this for two days,Please help.
If self is a subclass of UINavigationController, you do not need to refer to the navigationController property. (Note that this won't work if EditProfileViewController is also a subclass of UINavigationController, as you can't push a UINavigationController inside a UINavigationController).
EditProfileViewController *nextViewController = [storyboard instantiateViewControllerWithIdentifier:#"EPVController"];
[self pushViewController:nextViewController animated:YES];
Otherwise, if you don't have a pre-existing navigation controller
EditProfileViewController *nextViewController = [storyboard instantiateViewControllerWithIdentifier:#"EPVController"];
[self presentViewController:nextViewController animated:YES completion:nil];

Resources