dismissModalViewController multiple - iphone

So, I am using a RootViewController from which you can display first ViewController Categories and then from Categories you display next ex. Music
RootViewController -> Categories -> Music
In RootViewController I use this
[self presentModalViewController:categoriesView animated:NO];
to present the modal view and then dismiss it from Categories with
[self dismissModalViewControllerAnimated:NO];
From Categories to Music I use again
[self presentModalViewController:fruitView animated:NO];
to present the Music modal view and then dismiss it in music with again the same as above.
Is there a possibility to dismiss two modal views? I want a method that leads from Music back to RootViewController, dismisses both last modal views.
Any ideas?

Hi Use this Following code[[[self presentingViewController] presentingViewController] dismissModalViewControllerAnimated:YES];

Are you sure you want to use modal views for this? It sounds like what you're trying to do is better solved with a UINavigationController, where you can push and pop view controller's in a stack (and there's a popToRootViewControllerAnimated: message you can use).
This is how drill-down navigation is idiomatically handled in iOS (in the iPod, Notes, Contacts, Videos, Photos apps for example).
There's sample code for this in Xcode, I believe.

UINavigationController has a popToRootViewControllerAnimated: method, which per the documentation:
Pops all the view controllers on the
stack except the root view controller
and updates the display.

Use popToRootViewControllerAnimated method of UINavigationController.
[self.navigationController popToRootViewControllerAnimated:YES];

What you're talking about here, going from more general to more specific views, is better handled with a UINavigationController pushing and popping views. These are the views which slide left and right on the screen. Pushing means it slides in from the right (and shows a new, more specific view). Popping slides back to the right and shows a more general view.
A modal view controller is the one that slides in from the bottom of the screen. Look at the iPod app on your device for the way to handle this.

I use a nice utility method to do this... see here:
How to dismiss the two or more dismissModalViewController?

Use This, In music view write this for dissmiss 2 view .
[RootViewController dismissModalViewControllerAnimated:YES];
Here RootViewController is an object of RootViewController
Hope this will help u.

Related

Accesing the Views in iPhone

I implemented 4 views in AppDelegate depends on requirement it will call the view.I called second(example) view then I pushed to the new view again I pushed to the other new view.I want to go back to the second(example) view. I implemented the code like this [self.navigationController popToRootViewControllerAnimated:YES]; but it is poping to previous view and again it go back to the second(example) view. Can any one tell me how it goes directly to the second(example) view.
Thanks in advance.
Instead of using popToRootViewController you need to use popToViewController like bellow
self.navigationController popToViewController:secondViewObj animated:YES];
You should use
popViewControllerAnimated:animated]
or
popToViewController:yourVC animated:animated]
Use [pushViewController:obj animated:NO].....
Though this will not pop your view, but it will push your view to obj that is object of FirstView.
This will simply work as you want, may be not technical way...:)

NavigationController Issue on iphone

I am using navigation bar on my application for reach next views in my iphone application.
in a stage of view I want reach back my first view.How can i do it please give me suggestion.Thanks.
There is a popToRootViewControllerAnimated: method that will remove all controllers from the stack and return the user to the first view controller.
See the Apple docs.
[self.navigationController popToRootViewControllerAnimated:YES];
This call will take you to the root of your view controllers
You need to use to go back to your previous view
[self dismissModalViewControllerAnimated:YES];
Hope it helps.

Keep new view in a tab when switching to another tab

This is the situation:
I have a tab bar with 2 tabs. Tab01 and Tab02.
In Tab01 I have a button which pushes repVC:
repVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:repVC animated:YES];
[(UIViewController *)[tabController.viewControllers objectAtIndex:0] setView:repVC.view];
[repVC release];
Inside repVC I have another button which pushes an MFMailComposerViewController:
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
[self presentModalViewController:mail animated:YES];
[mail release];
The problem is: when mailView is shown(in Tab01) and I click Tab02, then back to Tab01, the mailView is hidden and even if I click the email button again, the view won't be presented.
So what I have is: Tab01.view -> repVC.view -> mail.view
For repVC, I use this line when I push the view so that even if I go switch tabs, that view will still be activated:
[(UIViewController *)[tabController.viewControllers objectAtIndex:0] setView:repVC.view];
But I can't do the same for mail because tabController is declared in another class which I cannot import. So I can't access the tabController and set the view for Tab01.
Hope the edit helped the understanding.
Hmm,
I still would suggest to use a Navigationcontroller. Would make things way easier, is conform to apple guidelines and suggestions and is pretty fast implemented. (Just create a Navigationcontroller, put the View of Tab1 as main view and hand it over to the TabbarController. Then for the mailView use [self.navigationController pushViewController:mail animated:YES]; Then the navcontroller "saves" the present view for you when u switch tabs)
But if for some Reason you have to use a modalViewcontroller you could either just deactivate the tabbar while the ModalView is shown or try to implement a switch or a simple if...else case in your ViewWillAppear where u check what screen to load.
Then Clean out the Window and load the right screen.
Hope you get the idea of what I mean, sometimes my way of writing seems to confuse people. ^^
A little more information would be great.
How did u set up your TabbarController?
How do u push the new view? Within a UINavigationController? If not, then do it with a navController, he should save the actual state of view and your problem should be solved.
If u already use a navController please post your ViewDidLoad and ViewWillAppear of the Viewcontroller of Tab 1
As #Amandir points out you could probably solve your problems by using a UINavigationController. I get a feeling that you are trying to abuse the modal view controller concept a bit and that's why it doesn't work as you expect. When you use presentModalViewController:animated: the intention should be that you are displaying a view that is modal, i.e. the user must interact and dismiss the modal view before she can continue.
What the paragraph above means that when you present a modal view controller it shouldn't be possible to use the tab bar. Since you are using the word push I'm guessing that you would like change the view of Tab01 while still being able to use the functionality of the tab bar. The problem is that there isn't any built-in method of pushing view controllers besides UINavigationController. persentModalViewController:animated: should only be used in case where you want a modal view, which on the iPhone means a full screen view.
The easiest way would probably be to use an UINavigationController and hide the navigation bar. Then you would get the functionality I think you are after. The other option is to manually add and remove sub views.
[self.view addSubview:repVC.view];
and
[repVC.view removeFromSuperview];
[self.view addSubview:mail.view];
You can use block animations if you want some fancy transitions.

popToRootViewControllerAnimated Question

I have a UINavigationController in my iPhone app controlling views in a drill-down fashion. I have 4 viewcontrollers that I pass through before I have a "Start over" button, where I would like the action to send the user back to the beginning of the view hierarchy.
[self.navigationController popToRootViewControllerAnimated:YES];
but it looks like it simply brings up a new rootviewcontroller on top of the current view controller.
Is there a way to pop the views back to the very beginning of the navigation drill down?
Any help would be great!
Many thanks,
Brett
Something else is going on here. I use popToRootViewControllerAnimated in on of my apps and it does exactly what you are after.
and if you want to goback direction to your desired view controller then use the code bellow-
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:3] animated:YES];
Here 'objectAtIndex:3' is the flow position(i.e. index) of your viewControllers. For the root it would be 0.

Switch between UITabBarController and UINavigationController

The main functionality of my app is controlled by a UITabBarController. However, I need to load a View that has a UINavigationController. When I return to my UITabBarController using
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
My UITabBarController no longer responds to clicks. It seems like the View does not have focus.
However, if I use this code to switch back to the UITabBarController:
[window addSubview:tabBarController.view]
My buttons will respond. I feel like "addSubview" is less efficient because I never remove the view from the window and therefore it must be adding a second copy of the view to the stack. Am I correct? Is there a way to use the first method and make my buttons respond? Please let me know.
It sounds like maybe you're presenting the Nav Controller incorrectly. You definitely shouldn't be adding views directly to the window. You want to present it using
[myTabBarController presentModalViewController:myNavController animated:YES];
When you're done with the nav controller you dismiss it with
[myTabBarController dismissModalViewControllerAnimated:YES];
and everything should work.
BTW, this is all documented in the docs for UIViewController and the "View Controller Programming Guide for iPhone OS" document.