Push ViewController in NavigationController from side menu - ios

I'm using a UINavigationController and I have a side menu in it. The problem is that when I want to push a UIViewController in the NavigationController from the side view it doesn't work. To do it, is use that code:
[self.slidingViewController resetTopView];
CDRecentChats* recentchats = [[CDRecentChats alloc]init];
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"conversation"];
[recentchats.navigationController pushViewController:controller animated:YES];
As you can see, I first close de side menu and then call the NavigationController to push a new ViewController. Using that code, the only thing that happens is that the menu is closed, but the NavigationController doesnnt push any ViewController. what can I do?

popToViewController means to go back to one that has already been made part of the navigation controller's stack. If you're trying to show a new one, use pushViewController.

Related

View hierarchy changed after push a view controller through navigation controller?

There is problem in view hierarchy. Here is flow of my app.
When app starts View Controller "A" is Visible. After that storyboard "B" is loaded through "StoryBoard Reference (Push)" ,Where another navigation controller is present and Home screen is loaded. On Click of Menu button in Home screen Side panel is visible.
Now When i click on side panel menu items view Controller "B" is pushed. This View Controller is Pushed Under Home screen and is not Vsible.
Help View contoller is visible under Home controller. I want Help View Controller should come on top of Home controller.
I dont understand what issue is coming..
Any Help will be appreciated..
If you are pushing view controller from side panel shows in image, will never push because it is not in navigation controller.
the answer depends on how you showing your side menu but from assumptions
what you need to do is to set root view controller or keep reference of navigation controller and push from there
If you are presenting side menu then it is not on navigation controller so you should first dismiss this side menu and on the completion of it you should push new view controller (says B or Help). I am writing my code snippet that i am using in my project for demonstration,
- (IBAction)settingClick:(id)sender {
SettingViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:#"settingsScreen"];
[self dismissViewControllerAnimated:NO completion:^{
[self.vc.navigationController.navigationController pushViewController:svc animated:YES];
}];
}
Above method push setting view controller on current navigation controller after dismissing side menu
Now important thing is self.vc this is the object of previous viewcontroller (Home controller in your case i think) on which side menu was presented.
So my SideMenuViewController has a property like,
#property (nonatomic,strong) UIViewController *vc;
which i am setting with self from previous view controller (in your case from Home view controller) something like,
SideMenuViewController *smvc = [self.storyboard instantiateViewControllerWithIdentifier:#"sideMenu"];
smvc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:smvc animated:NO completion:^{
smvc.vc = self;
}];
And i have used [self.vc.navigationController.navigationController pushViewController:svc animated:YES]; i.e. two navigation controller to push because i have two navigation controller in my view hierarchy to push this new view controller.
You can manage that as per your setup that how many navigation controller you have !!
Hope this will help :)
After digging a lot, I found solution for this.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Home" bundle: nil];
RootController *someViewController = [storyboard instantiateViewControllerWithIdentifier:#"RootController"];
[self.navigationController pushViewController:someViewController animated:YES];
I have set another navigation controller as root ViewController of the window and it worked for me.
Thanks.

Load a view that is previously loaded in iOS

I have been searching around about this problem, but haven't got any answer yet. My problem is: I have a push button that when clicked will push to another view.
Here is how I push it:
ViewController *vc = [[UIStoryboard storyboardWithName:#"Main" bundle:nil]
instantiateViewControllerWithIdentifier:#"Main_View"];
[self.navigationController pushViewController:vc animated:YES];
The ViewController vc is the view that I pushed. So in the new view, there is a Back button that can drop the current view and comeback to the old view. When I click the push button, the vc been load again from allover.
How can I make the instance of vc stay in memory, so when I go back to old view and come to vc again, it doesn't need to reload again?
I guess it is something related to navigationController stack, but don't know how to do that.
Thanks
Just keep a ViewController instance as property in your first ViewController
#property (strong,nonatomic)ViewController * vc;
Then when push it,check if it is in memory
if (self.vc == nil) {
self.vc = [[UIStoryboard storyboardWithName:#"Main" bundle:nil]
instantiateViewControllerWithIdentifier:#"Main_View"];
}
[self.navigationController pushViewController:self.vc animated:YES];
Are you using a UINavigationController? If so, can call the following line of code in you back button 'IBAction':
[self.navigationController popViewControllerAnimated:YES];
If you really need to keep a instance of the view controller, do what Leo suggested.

Push animation without navigation bar

I'm beginner developer apps I have stroyboard with 2 UIViewController can how do a push animation without navigation bar
You don't need a navigation bar for pushing. It is just a UI component.
You can choose to remove it by
[self.navigationController setNavigationBarHidden:NO animated:YES];
UINavigationController is the stack which enables pushing.
Hold the "control" key, and drag from viewControllerA to viewControllerB in the storyboard.
you need to embed your rootViewController into a NavigationController. Could make with storyboard. Click on your Controller. Editor -> Embed In -> NavigationController.
Then you can fire push-navigation.
UIViewController *controller = ...;
[self.navigationController pushViewController:controller animated:YES];

Present modal view controller from ECSlidingViewController

In my app I have an ECSlidingViewController declared as initial root controller via Storyboard. In my AppDelegate's didFinishLaunchingWithOptions method, I instantiate it as above:
self.slidingController = [[UIStoryboard storyboardWithName:#"AppStoryboard" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"ECSlidingViewController"];
What I want is to be able to show a global modal view controller (eg. when a push notification arrives while the app is active) without knowing which controller is currently top in the sliding controller.
What I do is (in my AppDelegate):
[self.slidingController.topViewController presentModalViewController:controller animated:YES];
but it doesn't seem to work.
Is there any way I could present a modal controller from my sliding controller regardless which controller is topViewController?
PS. If no, is there any chance that what I want will work with SWRevealViewController instead of ECSlidingViewController? If it's worth, I will take the painful road to switch.
Thank you in advance!
If the ECSlidingViewController is set as the initial view controller in the storyboard, then why are you instantiating another one in your app delegate code? By doing that, you're calling your methods on a different instance of ECSlidingViewController than the one that's put on screen by the storyboard. This is likely the source of your problem. Instead, get a reference to your ECSlidingViewController like this:
self.slidingController = self.window.rootViewController;
Then try,
self.slidingController.topViewController presentModalViewController:controller animated:YES];
or
self.slidingController presentModalViewController:controller animated:YES];
I haven't worked with ECSlidingViewController, so I don't know which of these might work.
Try this
UIViewController *rootViewController = self.window.rootViewController;
// You now have in rootViewController the view with your "Hello world" label and go button.
// Get the navigation controller of this view controller with:
UINavigationController *navigationController = rootViewController.navigationController;
[navigationController.topViewController presentModalViewController:controller animated:YES];

PushViewController iOS

I have three view controllers. MainViewController, secondViewController, thirdViewController. I would like to know once I click on done button on the thirdViewcontroller,
How could I go directly yo MainViewController instead of going back secondViewController and then MainViewcontroller by using pushViewController?
Assuming that your using a navigation controller, you can pop to root by calling:
[[self navigationController] popToRootViewControllerAnimated:YES];
This will bring you back to your MainViewController.

Resources