In my application mainviewcontroller is pushing modalviewcontroller by using this
[navigationController pushViewController:_viewController animated:YES];
For going back to mainviewcontroller using popviewController
[self.navigationController popViewControllerAnimated:YES];
Now as per apple reference documentation delegate is used by the presented view controllers to notify the presenting view controller when it is ready to be dismissed.
But in my case i am not using presentviewController and dismissmodalviewcontroller. It means my done button cannot use delegate to navigate back to mainviewcontroller like this
[delegate dismissModalView:self];
So what code i should write in donebutton method so that it uses
-(void) dismissModalView:(UIViewController *)viewController;
{
[self.navigationController popViewControllerAnimated:YES];
}
to navigate back to mainviewcontroller.
help will be appreciated.
Thanks
Try this to go to the rootView, the first view on the stack:
[self.navigationController popToRootViewControllerAnimated:YES];
Or to specify a viewController:
[self.navigationController popToViewController:YourViewController animated:YES];
Related
I had a tabbarcontroller application in which i am created that like this
UITabBarcontroller (UIWindow's rootViewController)
->UINavigationController (first tab)
-->UIViewController
->UINavigationController (second tab)
-->UIViewController
in the navigation bar i had a button in which i am pushing another view controller to the self .navigation controller.But now i want the same action from the other navigation controller also. so i need to push to the navigation controller which is the top view controller.I need to replace the [self.navigationController pushViewController:viewcontroller animated:NO]; with in a way that whichever is the navigation controller present there in the tabbar controller we need to add to that.Can any body help me on this?
Well how simpler than holding a refference to the UInavigationController that is currently visible (you should hold that refference from the UITabBarController delegate tabBarController:didSelectViewController:)
So you add in your AppDelegate a variable:
UINavigationController *navController;
then :
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (viewController.navigationController)
navController = viewController.navigationController;
}
Now, you do the push this way:
if (navController)
[navController pushViewController:viewcontroller animated:NO];
So at first you need have access to TabBarController. I think you can get it somewhere in AppDelegate.
then you can just call:
UIViewController *ctrl = myTabBarCtrl.selectedViewController;
[ctrl.navigationController pushViewController:viewcontroller animated:NO];
I want to make a back button from a modal view to the Main view.
The View Controller is embedded in the navigation controller. The menu button take me to the Second View Controller. There I have a back button who works fine using this:
[self.navigationController popViewControllerAnimated:YES];
I want to go back to the Main View Controller from the Page Two VC.
I have tried:
- (IBAction)goToRootView:(id)sender {
[self.presentingViewController dismissViewControllerAnimated:(NO) completion:nil];
[self.navigationController popViewControllerAnimated:YES];
}
and:
- (IBAction)goToRootView:(id)sender {
[self dismissViewControllerAnimated:(NO) completion:nil];
[self.navigationController popViewControllerAnimated:YES];
}
The first just goes back to the Second VC, the last sends and lldb error.
How can I go from Mantras Page Two VC to Main VC?
Thank you for your help!
You can tray this...
CHS_View_Controller *oldView = [self.storyboard instantiateViewControllerWithIdentifier:#"CHS_View"];
UINavigationController *yourNavigationController = [[UINavigationController alloc] initWithRootViewController:oldView];
yourNavigationController.modalTransitionStyle= UIModalTransitionStyleCrossDissolve;
[self presentViewController:yourNavigationController animated:YES completion:nil];
for that you must:
1) import you dest controller
#import "CHS_View_Controller.h" // your controller name
and
2)
set an Identifier for your CHS_Controller, "CHS_View" in the bellow example (into the Storyboard Editor and in the Attributes Inspector)
In the first snippet, instead of
[self.navigationController popViewControllerAnimated:YES];
try
[self.navigationController popToRootViewControllerAnimated:YES];
Im using Navigation Controller for my ViewControllers,I set my importantViewController as something like this to be its RootView:
UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController: vc];
[self presentModalViewController: navControl animated: YES];
Then, I pushView anotherView the FrontViewController like this:
[self.navigationController pushViewController:vc animated:YES];
After a button is pressed in FrontViewController another view will be pushed ViewA but it is connected with another ViewController ViewB the same way as this AGAIN:
[self.navigationController pushViewController:vc animated:YES];
(Which I think Im doing wrong when dismissing either of them with [self.navigationController popViewControllerAnimated:YES];)
This is an illustration:
My problem is, I need to navigate between View A and View B then when I dismiss either of them it will got back to FrontViewController. Like a child of a child View. Thanks.
I think this is for dismissModalViewController, but try this,
From View B write code like this
[[self parentViewController].navigationController popViewControllerAnimated:YES];
and From View A you can write,
[self.navigationController popViewControllerAnimated:YES];
Or either you can use this,
[self.navigationController popToViewController:frontViewController animated:YES];
UPDATE
for (UIViewController *tmpController in [self.navigationController viewControllers])
{
if ([tmpController isKindOfClass:[FrontViewController class]])
{
[self.navigationController popToViewController:tmpController animated:YES];
break;
}
}
This is the best solution to achieve this.
Write this code on both of your View A or B.
Hope it works now :-)
There is one way #Prasad G indicated. But problem with this solution is you need the same object of frontViewController. You can't do this with creating a new object. For going to this way declare frontViewController object in appdelgate and while pushing it from importantVC use
appdelgate.frontViewController = // initialize
// Push it
While going back from view B
[self.navigationController popToViewController:appdelegate.frontViewController animated:YES];
Another solution is
for (UIViewController *vc in [self.navigationController viewControllers]) {
if ([vc isKindOfClass:[FrontViewController class]]) {
[self.navigationController popToViewController:vc animated:YES];
break;
}
}
Using this way you can go on any of view controller from any level of navigation stack.
Using the first solution if you have 10 view Controllers and you want to go on any of one so you have to first create object of all 10 View Controller in appdelegate.
This code may have spell issues as I just typed this here
Hope this helps :)
UPDATE
->You have impVC as your root view
-> You pushed frontVC
-> From there you Pushed VC_A
-> From there you want to push VC_B
so you are done with pushing and for coming back to VC_A you can use
[self.navigationController popViewControllerAnimated];
Now you can again come on VC_B and again pop it. For going to frontVC from VC_A you can use popViewControllerAnimated and for going to frontVC from VC_B you can use the for loop i mentioned.
Please explain if you are looking anything else. If you are still facing issue please explain.
[self.navigationController popToViewController:frontViewController animated:YES];
Try like this i think it will be helpful to you.
In FrontViewController After a button is pressed:
ViewA instance
[self.navigationController pushViewController:ViewA animated:YES]
When ViewA disissmed
[self.navigationController popViewControllerAnimated:YES];
Load ViewB in ViewA
ViewB instance
[self.navigationController pushViewController:ViewB animated:YES]
On back viewB to FrontViewController
FrontViewController instance
[self.navigationController popToViewController:FrontViewController animated:YES];
On back viewB to viewA
[self.navigationController popViewControllerAnimated:YES];
I'm working on a view controller that can be presented modally or pushed into a navigation stack. I made it a UINavigationController subclass so that I get all the UIToolbar stuff for free. I can present it modally using:
[self presentModalViewController:myViewController animated:YES];
Problem is, UINavigationController doesn't allow pushing another UINavigationController into it (makes sense), so this crashes it:
[self.navigationController pushViewController:myViewController animated:YES];
Would there be a way to detect how myViewController is presented and automatically have it switch between UINavigationController and UIViewController accordingly so that I don't need 2 different classes?
In other words, myViewController would be able to detect how it's getting presented and pushing it would come down to something like:
[self.navigationController pushViewController:myViewController.topViewController animated:YES];
NOTE: Something like this would probably do, but it's getting too far away from the default UIViewController behaviors:
[myViewController pushIntoNavigationController:navController]; // only push myViewController.topViewController
[myViewController presentModallyInParentController:parentController]; // push the whole myViewController
In the myViewController subclass, create a method something like this:
- (void)presentFromViewController:(UIViewController *)presentingViewController
{
if ([[presentingViewController class] isEqual:[UINavigationController class]])
[presentingViewController pushViewController:self.topViewController animated:YES];
else
[presentingViewController presentModalViewController:self animated:YES];
}
Would this work or am I not understanding correctly?
So I have a tabBarController as a modalview, and it shows up fine. As I click some of the tabs, the views are loading properly. I want to dismiss the modalView when I click on tabBarController.selectedIndex ==4
So I write in the viewDidLoad and also tried in the viewWillAppear of that view controller to dismissModalViewController and it does not work.
I tried
[self.parentViewController dismissModalViewControllerAnimated:YES];
// ... And also //
[self dismissModalViewControllerAnimated:YES];
Could someone point out why it does not work ?
All you have to do is pass a reference to the modally presented VC pointing on the VC that will present it modally.
Define a weak reference as a property in the UITabBarController subclass, and send a message to dismiss it when required.
For example using a property named mainViewController :
MySubclass *tbController = [[MySubclass ....];
tbController.mainViewController = self;
[self presentModalViewController:tbController animated:YES];
Then in MySubclass define
#property(assign) UIViewController *mainViewController;
and synthesize it, then when the tab you want gets selected :
[self.mainViewController dismissModalViewControllerAnimated:YES];
I think the 4th view controller (of the tab bar controller) is trying to get dismissed by the line
[self.parentViewController dismissModalViewControllerAnimated:YES];
Since this 4th view controller was not presented by any controller, this wont work.
And it is dismissing it's modal view controller by the line
[self dismissModalViewControllerAnimated:YES];
Since, this 4th view controller did not presented any view controller, this again should not work.
You want to dismiss the tab bar controller and not its 4th view controller.
Basically, you can get the reference of tab bar controller from the 4th view controller.
As, [yourFourthViewController.tabBarController.parentViewController dismissModalViewControllerAnimated:YES];
I am guessing this without actually trying. Let me know if this works.
If you have the UINavigationController as the parent controller then the following line will work for you.
[self dismissModalViewControllerAnimated:YES];
But here I think you have the UIViewController is the parent controller instead of the UINavigationController. So, You can do one thing when presentModalViewController.
if(objView == nil)
objView = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:objView];
[self presentModalViewController:navigationController1 animated:YES];
Let me know if you need more help or any questions.