Pop to the main view controller - iphone

In my app I've 2 view controller that they appears by using a modal transition, now I developed the 3rd view controller and I put a button to go back to the main view controller, but how I can go back to the main view controller?
I tried with this:
[self dismissViewControllerAnimated:YES completion:nil];
but with this code I go back to the 2nd view controller. How I fix it?
Thank you!

Assuming your first modal presented the second modal, the following should work:
__weak UIViewController *vcThatPresentedCurrent = self.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^{
[vcThatPresentedCurrent dismissViewControllerAnimated:YES completion:nil];
}];

Try with this....
[self.presentingViewController.presentingViewController
dismissViewControllerAnimated:YES completion:nil];
Let me know if you have any problem.

Related

navigation bar is hidden when i return back to view in iOS

I am new in iPhone development. I created an app which use a navigation bar using storyboard. My problem is that i am opening a viewB programmatically from viewA on button click and it successful. Now to go back to viewA i have used cancel button. when i click on cancel button (previous) the (viewA) is opened but navigation bar is not shown. and viewA have navigation bar control but viewB does not.
Thanks in advance
View A
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
EditViewController *viewController = (EditViewController *)[storyboard instantiateViewControllerWithIdentifier:#"EditViewController"];
[self presentViewController:viewController animated:NO completion:NULL];
View B:
- (IBAction)cancelButtonPressed:(id)sender {
if ( lables != NULL) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ScannerViewController *viewController = (ScannerViewController *)[storyboard instantiateViewControllerWithIdentifier:#"ScannerViewController"];
[self presentViewController:viewController animated:NO completion:NULL];
}
else{
[self.navigationController popViewControllerAnimated:YES];
}
You are presenting the viewB & popping it using self.navigationController, you should use one way, either use presentviewcontroller & dismissviewcontroller.
[self dismissViewControllerAnimated:YES completion:nil];
For your scenario it is best to use UINavigationController
e.g
Pushing:
[self.navigationController pushViewController:viewController animated:YES];
Closing
[self.navigationController popViewControllerAnimated:YES];
You have to use a navigation controller, in your StoryBoard select your view A, Editor Menu > Embed In > Navigation controller.
http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html
If you are using storyboard no need to present the viewController programmatically. press and hold ctrl, drag from viewCA to viewCB and select model from popup menu.
For dismissing the viewController
[self dismissViewControllerAnimated:YES completion:nil];
If you want to send any data to viewCB give segue identifier (for example "segid")
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"segid"])
{
//write your code here.
}
}
This method will get called automatically (delegate) while presenting (of pushing) viewController.
I was also facing the same problem. The solution is to never select the modal on click on the back button, because modal covers your whole view and that's why on the back screen navigation controller is not showing. So don't make connection for back button, just write the code for back button.
[self dismissViewControllerAnimated:YES completion:nil];
When you needed (e.g. viewDidAppear method) use,
[self.navigationController setNavigationBarHidden:NO animated:YES];

PresentViewController from AppDelegate

I am trying to present a ViewController which I know can be done with the following line.
[self presentViewController:myVC animated:YES completion:nil];
However I have a part which is calling the AppDelegate and that is where I run into trouble.
I can manage to sort it out with:
[self.navigationController pushViewController:bvc animated:YES];
but as the code implies, this does a navigation push - how do I it, so that I can do a vertical push up?
You can present on navigation controller -
[self.navigationController presentViewController:myVC animated:YES completion:nil];

Back to main view controller from modal View controller

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];

iOS: Replacing modal ViewController with UIModalTransitionStyleFlipHorizontal

I present a modal ViewController using presentViewController. Now I want to flip this modal view using UIModalTransitionStyleFlipHorizontal so from within the presented ViewController I call:
flipsideView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:flipsideView animated:YES completion:^{}];
The animation looks fine. But when I dismiss the flipsideView first modal view is still visible.
Is there a way to dismiss both modal views at once using the UIModalTransitionStyleCoverVertical. Or is it possible to replace the first modal view with the flipsideView using the flip-animation?
Thanks!
You can go back to the first controller like this, from a button in the flipsideView:
-(IBAction)goBackToFirst:(id)sender {
self.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

dismissViewControllerAnimated is called but ViewController is not dismissed

I am having a problems with the dismissViewControllerAnimated method not closing down the view.
What is happening in the app here is:
Cell in ItemViewController is selected.
View is pushed to ItemDetailViewControllerand details are sent through a delegate
User selects 'done' and the event is sent via a delegate to be closed in ItemViewController
All of this works except for the View is not dismissed, there are no errors. Can anyone see what is wrong?
- (void)itemDetailViewControllerDidFinish:(ItemDetailViewController *)controller
{
NSLog(#"Controller: %#", controller);
// Returns - Controller: <ItemDetailViewController: 0x6b68b60>
[self dismissViewControllerAnimated:YES completion:nil];
}
What if you call [controller.navigationController popViewControllerAnimated:YES] instead?
For that matter, what if you call [controller dismissViewControllerAnimated:YES completion:nil] instead of calling it on self?
The answer is in this page:
dismissviewcontrolleranimated-vs-popviewcontrolleranimated
dismissViewController is used when you do not have a navigationcontroller.
Most probably you are using a navigation controller, then use
self.navigationController popViewController instead.
Also take note of lemax his remark: use NULL, not nill for the completionhandler
I had a problem in iOS5 where the standard completion callback was not allowing the view to completely dismiss (only the current pushed view of that modal)
[controller dismissViewControllerAnimated:YES completion:^ {
//
}];
Solution for iOS5 is to not have a callback:
[controller dismissViewControllerAnimated:YES completion:nil];
Had a problem where calling dismissViewControllerAnimated dismissed the keyboard in a UIViewController, but not the view itself.
Solved it by using two calls:
[self dismissViewControllerAnimated:NO completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];
an instant one for the keyboard, then an animated one for the controller
Your Situation is -
ItemViewController -> ItemDetailViewController
(pushed on navigationController)
Self.dismissViewController(..) dismiss a view controller that is presented over self(in ur case it is ItemViewController). Here, u did not presented any VC over self, instead u pushed a new VC over navigation stack. So, Correct way to dismiss ItemDetailViewController would be
self.navigationController.popViewController(true). please read the description of dismissViewController(....) to get more clarity.