How can I push view controller with storyboard identifier using LGSideMenuController? - ios

I have to push a detail view controller when I tap on a tableview cell using with storyboard identifier. I have already designed the view controller. Now i have to navigate to the designed screen by tapping table view cell in the left menu. I am using LGSideMenuController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
if (indexPath.row == 0) {
ProfileViewController *profileVC = (ProfileViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:PCProfileVC];
[self.leftMenuVC navigateToViewController:#"profileVC"];
}}
-(void)navigateToViewController:(UIViewController*)viewController{
[(UINavigationController *)[self sideMenuController].rootViewController pushViewController:viewController animated:YES];
[[self sideMenuController] hideLeftViewAnimated:YES completionHandler:nil];}
Please help me to do. Thanks

Why are you pushing viewController in rootViewController.
If this code snippet is written in your ViewController.m, then try replacing,
[(UINavigationController *)[self sideMenuController].rootViewController pushViewController:viewController animated:YES];
with
[[self sideMenuController].rootViewController.navigationController pushViewController:viewController animated:YES];
If your identifier PCProfileVC is correct, then this code should work.
Kindly see this link for information on pushing ViewController in UINavigationController.

Try this code :
YourViewControllerClass *viewController = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"ViewController"];
// instanciate your viewcontroller
[(UINavigationController *)[self sideMenuController].rootViewController pushViewController:viewController animated:YES]; //push your viewcontroller
[[self sideMenuController] hideRightViewAnimated:YES completionHandler:nil]; //hide the menu

Related

Cant navigate to other Viewcontrollers from a commonViewController's tableview click

I have a commonViewController to pop up on other viewcontrollers and the commonViewController has a view and a tableview inside it and also some other components. I have populated that view successfully on other viewcontroller pages, but the problem is; when iam trying to navigate to some other viewcontroller pages while iam clicking the rows in commonViewController's tableview.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 1)
{
EditProfileViewController *nextViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"EPVController"];
[self.navigationController pushViewController:nextViewController animated:YES];
}
}
I have tried both presentViewController and pushViewController but it is not navigating anywhere. What is the issue. Please help me.
Get reference to your storyboard and load viewcontroller using identifier:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"YourStoryboardName" bundle:nil];
EditProfileViewController *nextViewController = [storyboard instantiateViewControllerWithIdentifier:#"EPVController"];
[self.navigationController pushViewController:nextViewController animated:YES];

How to get the navigation bar on view controller?

I have three View controller A,B,C.I have navigation controller attached to A view controller.In A i have some buttons,I have attached the button segue to B view controller.Onclick of the button i go to the B view controller.On B View controller i have UITableView on click of table view item i am launching the C view controller.below is the code for that
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==0)
{
NSLog(#"first cell");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"BusinessCard"];
[self presentViewController:vc animated:YES completion:nil];
}
else if(indexPath.row==1)
{
NSLog(#"second cell");
}
else if(indexPath.row==2)
{
NSLog(#"third cell");
}
}
But on C view controller the navigation Bar is not appearing.I think C view controller is not linked to the Navigation Controller.
You use navigationController push method to display navigation bar in C viewController
try this code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"BusinessCard"];
[self.navigationController pushViewController:vc animated:YES];
Use the code below
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"BusinessCard"];
UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:vc];
[self presentViewController:nav animated:YES completion:nil];
You need to present it using UINavigationController as modal.
Hope it helps.
use this:
[self.navigationController pushViewController:vc animated:YES];
You can use "Push" segue and embed your ViewController in Navigation Controller, and then use its Navigation Controller's functions like pushViewController and popViewControllerAnimated
Answer are all correct, but i just want to explain things..
//This line gets the storyboard with the name "Main" which contains all
//the setup you made for UI (User interface)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
//While this like gets the view inside your storyboard with
//storyboard ID/indentifier `BusinessCard `
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"BusinessCard"];
//Lastly, this line is correct presenting the viewcontroller BUT this doesn't add your
//viewcontroller to the array of viewControllers inside navigationController
//
//Also, this line makes you present the viewController above the
//rootViewController of window which is in your case the navigationController
//
This is you Error
[self presentViewController:vc animated:YES completion:nil];
//This is what you are looking for, and the correct one for your implementation
//
//This will let you add the `vc`(viewController) to the array of viewController
//in navigationController, to confirm that you can check the `self.navigationController.viewControllers`
//which will return the array of viewController inside your navigationController
This is the Answer
[self.navigationController pushViewController:vc animated:YES];
Hope this helps you, and my explanation is apprehendable.. Cheers

While pushing a viewcontroller there is a jerk in animation

For pushing a view controller I'm using following code. On tap of UITableViewCell need to push DetailViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row)
{
NSDate *object = self.objects[indexPath.row];
DetailViewController *controller = [DetailViewController new];
[self.navigationController pushViewController:controller animated:YES];
}
}
Now problem is I get a jerk while animating. Refer to screenshot
its because of transparency. just set a background color for your viewcontroller. thats all.
Most probably you're doing something "heavy" on the main thread, while loading DetailViewController.
Could you show us what are you doing in DetailViewController?
I think there is issue with allocating object and pushing same object.
Do the following changes, it will work fine.
Your code :
DetailViewController *controller = [DetailViewController new];
[self.navigationController pushViewController:controller animated:YES];
Replace :
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main"
bundle: nil];
MyViewController *controller = (MyViewController*)[mainStoryboard
instantiateViewControllerWithIdentifier: #"<your Storyboard ID>"];
it will work fine at my end.
Hope this help you.

How to go to particular viewcontroller from leftviewcontroller in IIViewDeck in storyboard?

I am having difficulty using IIViewDeck in my app. I am using storyboard. I have managed to bring leftviewcontroller which is a tableviewcontroller with cells that are supposed to segue to particular viewcontroller in the storyboard. How do I do this? I have put this in appdelegate.
UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController *menuController = [mainStoryboard instantiateViewControllerWithIdentifier:#"mainSideNavMenu"];
UINavigationController* navigationController = (UINavigationController *) self.window.rootViewController;
self->viewDeckController = [[IIViewDeckController alloc] initWithCenterViewController:navigationController leftViewController:menuController rightViewController:nil];
self.window.rootViewController = self->viewDeckController;
You can manage it with code, with the didSelectRowAtIndexPath method of your table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
if (indexPath.row == 0) {
MyTripsViewController* newController = [sb instantiateViewControllerWithIdentifier:#"ViewController1"];
[self.viewDeckController closeLeftViewBouncing:^(IIViewDeckController *controller) {
self.viewDeckController.centerController = newController;
}];
}
if (indexPath.row == 1) {
MyTripsViewController* newController = [sb instantiateViewControllerWithIdentifier:#"ViewController2"];
[self.viewDeckController closeLeftViewBouncing:^(IIViewDeckController *controller) {
self.viewDeckController.centerController = newController;
}];
}
else {
MyTripsViewController* newController = [sb instantiateViewControllerWithIdentifier:#"ViewController3"];
[self.viewDeckController closeLeftViewBouncing:^(IIViewDeckController *controller) {
self.viewDeckController.centerController = newController;
}];
}
}
So you have to defined you row / viewController couple.
I know it's a bit late, but someone might find the answer useful at some point.
Max's answer is generally a good answer. However, the original poster's question is a bit vague. He/She didn't specify if the transition is required in the center view controller or the left one.
Assuming the transition is in the left view controller:
If you want push transition, you can use a code like:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
UINavigationController * nc = (UINavigationController *)self.viewDeckController.leftController;
UIViewController * vc = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"yourView"]; // use the name of the view (not the navigation view controller)
[nc pushViewController:vc animated:YES];
}
}
If you want Modal transition, just replace the code within the previous if statement clause with the following code:
UIViewController * vc = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"yourViewName"]; // don't use the name of a navigation controller
UINavigationController * nc = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentViewController:nc animated:YES completion:nil];
The navigation controller in this code is used to get the navigation bar shown. If you don't need the navigation bar, just omit the navigation controller definition and pass the view controller as a parameter instead in the presentViewController: animated: completion: method.
Assuming the transition is in the center view controller:
You can use the code Max had provided. Keep in mind that the way he showed the new view controller is by replacing the existing center view controller. The new view controller is neither pushed nor modally showed. If you want to push new view controller to the existing center view controller, you should use the same way I introduced the transition in the previous two pieces of code above. So, it should look like this:
Push Transition: (the following code should replace the code within the if statement clause in the first piece of code)
[self.viewDeckController closeLeftView];
UINavigationController * nc = (UINavigationController *)self.viewDeckController.centerController;
[nc pushViewController:[[[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"yourViewName"] parentViewController] animated:YES];
Modal Presentation: (the following code should replace the code within the if statement clause in the first piece of code)
[self.viewDeckController closeLeftView];
UINavigationController * center = (UINavigationController *)self.viewDeckController.centerController;
UIViewController * vc = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"yourViewName"];
UINavigationController * nc = [[UINavigationController alloc] initWithRootViewController:vc];
[center presentViewController:nc animated:YES completion:nil];

Cannot load viewController using MFSidemenu

I am building an app using storyboard. I am using the MFSidemenu library for creating a sidemenu like facebook app. the side menu appears properly but when i tap a cell on the side menu, the menu doesnt go away and it doesnt load another view controller. Please note that i am using storyboard.
Thank you.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]];
AlbumsTableVIewController *AlbumsViewController = [storyboard instantiateViewControllerWithIdentifier:#"Albums"];
AlbumsViewController.title=[NSString stringWithFormat:#"Demo Controller #%d-%d", indexPath.section, indexPath.row];
NSArray *controllers = [NSArray arrayWithObject:AlbumsViewController];
self.sideMenu.navigationController.viewControllers = controllers;
[self.sideMenu setMenuState:MFSideMenuStateClosed];
}
you have to implement delegate method to your MainMenu Controller, because if u place your code like above, it will change your sideMenu not ur MainMenu. i am working with storyboard here. this is my code on my MainMenuController
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]];
UINavigationController *navigationController = (UINavigationController *)self.navigationController;
UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"leftSideMenuViewController"];
UITableViewController *SideMenu=(UITableViewController *)leftSideMenuViewController;
SideMenu.delegate=self; //here is the delegate
[MFSideMenu menuWithNavigationController:navigationController
leftSideMenuController:SideMenu
rightSideMenuController:nil];
so you need to hook up with your viewController from here, navigating thru delegate pattern.

Resources