What is the correct way to handle multiple view controllers on the same screen? - ios

I'm having a view controller, that has few controls and images, and is located on top of the screen. I will have a space below it for child view controllers and their views.
I will show one child view controller at a time, what is a good way to do that?
Total amount of child view controllers is around 6, they are very different, so reusing some container view controller won't really work.
When pressing some button on these controllers I will move to next one.
Should I make some property, let's say contentView that will hold a view of the controller that is currently on screen?
How do I handle rotation if I don't use auto layout?
EDIT: This is more a theory question, I know of methods addChildViewController and know the way how I add views and controllers to their parents. I just want to know the good way to do that.

It depends somewhat on how you want to transition between the different child view controllers, but your question already lists a good approach.
You definitely want a different view controller for each of the children. Add a container view to your top level view. This view is where rotation and auto-resizing are handled. The contents of this view could be the child view controllers views themselves (and you control the transitions using one of the methods like transitionFromView:toView:duration:options:completion:) or the container view could hold a UINavigationController and you just push the child view controllers into it.
Whatever the container view holds, you need to take care if any of your child view controllers tries to present another view controller as a modal. The presented view controller needs to be presented by the view controller at the top of the hierarchy or the presented view may no interact correctly or truly be presented in front of other screen elements.

Related

Modal view that does not prevent access to parent

I am trying to create a two ViewController solution where a modal view controller is presented over a UICollectionView while allowing the user to interact with the CollectionView. In this case, it is like an advanced picker, allowing the user to choose items that will populate the properties in the modal view before saving a record.
I have a presentation controller setup to present the view how and where I want, allowing full visibility to the parent view. Nothing I have tried will allow the user to interact with (scroll, tap, etc) the UIController view.
In view debugging, I see a UITransitionView that has a frame equal to the full screen. (see image) I suspect that this is the culprit. Is this even possible in iOS?
The whole point of a modal view controller is that it takes over the screen and demands that the user respond to it before doing anything else. It puts your program into a "mode" that must be dismissed before the user can go on. That is the core reason for being of a modal dialog.
If you can interact with the view controller underneath the the top view controller is no longer a modal.
What you are trying to do is wrong from a human interface standpoint, and not supported by the application framework. You need to rethink your design.
Edit:
Top-level view controllers are not designed to share the screen. If you want another view controller to cover part of the screen while the user can still interact with the view controller underneath then you should use a container view as #МаксудДаудов suggests in his answer.
I would probably put a container view on top of the rest of my view controller's content, control-drag an embed segue to the child view controller I want to display, add an outlet to the container view, and then hide the container view.
When you want to display the "picker", you could then un-hide the container view, which would reveal the child view inside and let the user interact with it, while still being able to interact with the other components in your main view controller.
There is no way allowing presented full screen view controller to interact view controller under that. Instead , add your second view controller at containercontroller at some part of first, and change first VCs collection view frame accordingly, to be able see all the list. Doing this, you will have two view controllers working together

Issue with Container Views in XCode

I have a simple project with one view controller. I want to add two views and create a transition between the two.
I've read that I must add the two views to a container view. When I drag the container view to the Document Outline, It appears at the same level as the views. I can't get it to be higher in the hierarchy. What am I missing?
Thanks!
Martin
"container view" in this case does not mean the container view you can select from the object library (this one you need if you want to embed viewcontrollers in other viewcontrollers) but just a regular uiview. drag a regular uiview from the object library to your viewcontroller and put the views you want to animate into this container uiview.
You need to explain what you want to do more clearly. You also need to be careful to use the right terms. Views and view controllers are different animals. Pretty much everything that appears on the iOS screen is a view: Buttons, labels, text fields, switches, etc are all views. The object that manages a whole screen-full of views is called a view CONTROLLER. Do not call view controllers views.
If you want a transition where 1 view controller gets fully replaced by another, you don't want container views. You want to use a navigation controller or maybe a modal segue. If you want a second view controller's view to appear inside your first view controller's views, then you want a container view.
If you just want to add additional views (text fields, image views, buttons, etc.) to your view controller then you just drag those objects onto your first view controller's scene in your storyboard.
In order to provide more help than that you're going to have to be clearer about what you are trying to do.

Partial segue to show settings view

in the google maps app for ios. When you select the settings button, it will show you a view of options such as "traffic", "public transit", etc.
My question is how this is done on ios.
I tried following this tutorial but it says that it won't work on uinavigationviewcrollers. I have seen this partial segue of the the view in apps that use a navigational controller. How do they create that?
It's not a partial segue. It's not a segue at all, it use of containment view controllers.
Instead of a single view controller which transitions to a different view controller image one single master view controller. For simplicity, we'll say this view controller has two views (of the root), both of which cover the the whole screen. For this example let's think of them as "main" view and "menu" view.
Other than these two empty views, the view controller has no content. That's because this view controller does nothing other than manage other view controllers which get stuck into the two views. It will have a couple methods manage them, like presentInMainView:(UIViewController *)viewcontroller and presentInMenuView:(UIViewController *)viewcontroller
When the program starts running the master view controller will programmatically add the map to it's "main" view. The map view controller now cover the whole screen and looks and acts like it's the top level view controller, but it isn't. It's contained. At some point some taps the settings button and the map view controller will make a call to it's parent and say presentInMenuView:... and the master view controller will then load up a second view controller into the menu view. The menu view could even be located off the left side of the screen and the master view controller animates the menu view frame to side it right covering the whole screen. Assuming the menu view controller only has content which covers the left half of the screen you'll see the map view controller hiding behind it.
That really only scratches the surface, lots can be done with container view controllers. You could create a container which lets you brings up a dozen different views all populated with view different view controllers. You could size and arrange them on all over the screen and each child view controller could still only have to deal with it's own contents.
For more info there is the Apple Developer Guide and the WWDC 2011 Videos where it was introduced (session 102)
I used SWRevealViewController For similar type of sidebar animation.They given the good example of how to use SWRevealViewController also please try it once.

Add StatusBar Like View in App?

In my app I want to add a view much like the status bar, always onscreen at the top of each of my view controllers, displaying application wide data.
I really have no clue how I might achieve this so any suggestions would be really helpful. I;m sure someone must have chased this idea at some point?
Thanks.
What you're willing to do is fairly straightforward since iOS 5 using view controller containment, it allows you to embed child view controllers in a parent view controller.
In your case I would create a custom container view controller with two subviews: the content view and the statusbar-like view. The content view should display your current root view controller by adding it as a child view controller to your container view controller and adding its view as a subview to the content view. The statusbar-like view can then be used to display information that will be visible everywhere in your app.
You might want to read this documentation for further details:
Creating Custom Container View Controllers
You can create your custom view and add it as a subview to your keyWindow. This way it will always be visible. Another option is if you have tab bar controller or a navigation controller as your root view controller, then you can add that view as a subview to their respective view and it will always be visible.

Objective-C - Understanding view controllers

I understand that view controllers help control multiple views in an application, but I have trouble understanding when to use them.
If I have an application with a main page, several views with a "hierarchy" structure, and an about page not connected with the hierarchy, what files should my application have? An appdelegate, navigation controller and view controller? More than one view controller? Just a navigation controller?
Also, should they all be contained in one .xib file, or multiple .xib files?
Any help would be greatly appreciated.
Thanks.
A good habit is to have a UIViewController for each page you want to show. If I get the structure of your app you should have a main page (with many other UIViews inside it) and another page (about page). If that's true I suggest two UIViewControllers.
The UINavigationController is a subclass of UIViewController that lets you "navigate" among the pages. It's not strictly necessary but suggested (you can also implement your self a custom navigation system, but it's easier to exploit the one Apple offers you). Another navigation system is the one based on UITabBarController, if you want to take a look.
Assuming I get the structure of your app you should need two .xib file, one for each page you have.
The app delegate is conceptually different from a view controller, you'll have just a single app delegate, automatically created by Xcode (you can, of course, modify it to fit your needs).
Each "screenful of content" (Apple uses this term) should be handled by it's UIViewController or more likely a subclass of it. The point of view controller is to handle view appearing or disappearing (going on/offscreen), device rotation, memory management, navigating to other view controllers and so on. If you are creating your UI with IB, then each of those view controllers would most likely have it's own .xib file.
Each view controller has one view (it's view property) that acts as main view for each "screenful of content" to which you then add your subviews.
UINavigationController and UITabBarcontroller are there to help you control the hierarchy of your app. They only act as containers for other view controllers and don't contain any UI except navigation bar or tab bar. Using tab bar controller you can have multiple view controllers which act exactly like browser tabs. Using navigation controller you can have a stack-like navigation where new view controllers are pushed from right to left and are popped from left to right when user goes back to previous view controller. You can even have a view controller inside navigation controller inside a tab bar controller.
If you don't want to use tab bar or navigation controller, you can navigate through your view controllers by presenting them modally using presentModalViewController:animated: and dismissing by dismissModalViewControllerAnimated:. If you send YES for animated parameter of these methods, you will get an animation specified by the modalTransitionStyle property of view controller being presented or dismissed. Possible animations are slide in from bottom (default), horizontal flip of entire screen, fade in/out and half-page curl.
There are also some Apple-provided subclasses of UIViewController that help you setup your UI quicker like UITableViewController which is basically a view controller that contains a table as it's main view and conforms to 'UITableViewdataSourceanddelegate` protocols which are required to define how each cell looks and what it contains.
On iPad there is one additional container controller UISplitViewController and one additional way to present new view controllers using UIPopover.

Resources