PresentViewController from AppDelegate - iphone

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

Related

Pop to the main view controller

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.

Objective C Segues, dissolve programmatically

I have two classes, machineClass and buttonsClass... From buttons class, I want a button to programmatically push into the machineClass.
Ive gotten that to work with:
machineClass *mc = [self.storyboard instantiateViewControllerWithIdentifier:#"machineClass"];
mc.passedString = purchase;
[self presentViewController:mc animated:YES completion:nil];
However, this uses the default segue animation (where the view comes up vertically), and I think my app would look so much nicer if I used the cross dissolve transition instead.
Can anyone help? The best solution I've come up with is to change it to:
[self presentViewController:mc animated:NO completion:nil];
But I don't like this as much...
Thanks!
You can set the target view controller's modal transition style in the storyboard. Or in code:
machineClass *mc = [self.storyboard instantiateViewControllerWithIdentifier:#"machineClass"];
mc.passedString = purchase;
mc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:mc 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];

Navigating between 2 ViewControllers

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

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.