Multiple view controllers in a single scene? - ios

I've been a Mac programmer since 1993 and find it somewhat hard to get into the iOS frame of mind from time to time.
I've got an iPhone app that was designed for the iPhone 5 screen size and shows 3 different views with different functionalities depending on the orientation of the device (portrait and 2xlandscape).
Now on the iPad I want to separate the landscape screen into a left part controlled by one (existing) view controller and the right part by another (existing) view controller.
As part of trying to support split screen multi-tasking I want to support size classes but my views are in xib files at the moment.. and as far as I know size classes are bound to using storyboards, so I'm migrating to storyboards which seem very ill-structured for my purposes.
I can see that the idea is
1 window = 1 root controller
and
1 screen full = 1 scene = 1 view controller.
But what I need is two (or 3) view controller in a single scene.
I've had a look at UISplitView, which seems perfect only that the left view controller has to be a navigation controller, so not what I need. Perhaps a custom collection view controller is the answer?
I'm sure I'm missing something as I'm very much stuck in a Mac mindset and haven't used storyboards before.. what's the best way of re-using my existing view controllers in this scenario?
Any advice would be very much appreciated.

Take a look at this image:
There is a component in the object library called a container view. You can connect a new view controller via a segue to that container view. Basically, you can create a parent view controller whose sole purpose is to manage/move the views of other view controllers.
The parent view controller can have outlets to container views (which are simple UIView objects) and each container view can be its own view controller.
Hope this make sense, and sends you in the right direction.

Related

Small view displaying data in real-time on multiple views

I would like to create a small view under my navigation bar where i would display statistics based on the user utilisation of my app and those statistics could evolve in real-time.
I want to be able to "inject" this small view in lot of different views from my app just under the navigation bar.
I don't know how to proceed to create this small view that can use in different bigger views. And how i can interact with this view and others.
I read and tried things concerning xib files, but not sure i'm going in the right direction.
I'm thinking of using containers view but i'm not sure i can be able to "re-use" it in another view.
I'm a bit lost and i would like some guidance on how to make that in the best and flexible way.
Edit: I'have been trying with containers view. Here is what i did for the moment, i don't know if it's the right/best method.
In my two main view controller i got a container who embed the small view container where i'm gonna put my statistics.
Thanks !
I think you're on the right path with containers.
I suggest creating a view controller that has the view you want always displayed and a container view that takes up the rest of the screen space. This is your container view controller.
Each other view that you want to show inside the container area would be managed by a child view controller. This way, the controller with the most-persistent view is the parent of the ones that change.

Container controller view sizing

I'm currently building a custom view controller container for my iOS project and I nearly understand what I'm doing. The one thing I'm stuck on is how do you (properly - aka not a hack) add a view controller in PART of the Parent controller's frame. See how there are multiple view controllers/views in the email app? How does one build a custom controller container that designates the location of such sub-view controllers? How do you properly add such a controller? I'd like to know the "correct" way as designated by apple (best practice).
EDIT: After looking at this some more I was thinking a possible way would be to create views with custom sizing and then push those to the parent. Is this the correct way?
You sort of answered it yourself. The view you have as an example uses a UISplitViewController to show two separate views (left and right). Each of those views has a view controller that owns it. Note that the left side includes all of the view, like the search, nav bar, and toolbar. So just create two separate view controllers and add them to a UISplitViewController and you should be golden! The views themselves are created however you normally create views. Storyboards, NIBs, or in code works.

Nesting view controllers inside a storyboard

I'm learning how to use storyboards for iOS development and I can't seem to figure out how to manage multiple views on the same screen.
I want to be able to control multiple elements on the same screen, for example a UITableView with an UIImageView next to it.
This means I need 3 UIViewControllers, 1 for the UITableView, 1 for the UIImageView, and one for the main view. But as far as I can tell storyboards only allow for 1 controller per view hierarchy screen. Can anyone tell me what I'm missing?
View Controller Containment is the way to go. Have a look at Apple's documentation about it:
Creating Custom Container View Controllers
Container view controllers are a critical part of iOS app design. They
allow you to decompose your app into smaller and simpler parts, each
controlled by a view controller dedicated to that task. Containers
allow these view controllers to work together to present a seamless
interface.
You can use it in storyboards as well by using a container view.
Useful tutorial: Storyboards With Custom Container View Controllers

SplitView with multiple ViewControllers - Storyboards - iOS

I have finished iPhone version of my app and want my app to support iPads as well. I used a tabbar controller for iphone.. I could use the same for iPad, however, I would have too much free space on iPad if I use a Tabbar. so I have decided to use Split View Controller. Left part(table view) should be visible all the time even if it's not in landscape mode. And every time a cell is clicked, the corresponding view should be loaded to the right hand side.. By the way I am using storyboards.. Seems like it makes everything more difficult. Are there some examples of it? Thanks..
I have faced a similar situation recently. Basically you can use the split view project template to generate the basic code. After that, I created a DetailedViewControllerContainer interface and used it as the view controller for the right side view of the split view, replacing the generated DetailViewController.
After that I created several view controllers, each of them corresponds to a selection in the left side view(master view controller). And add these view controllers as the child view controllers for the DetailedViewControllerContainer.
The catch is that you will need to use code to load the child view controllers. The benefit is that the child view controllers do not need to be modified from the iPhone version. The DetailedViewControllerContainer remains the only SplitView delegate.
Take a look at the sample code I wrote on github:
https://github.com/raoying/SplitView-Sample

iPad Navigation Patterns

I've been working on an iPad application that has about 15 view controllers. Some of the view controllers are full screen, others are embedded inside the other view controllers (think split view controller).
On the iPhone navigation is very straight forward. Even if you have a ton of view controllers, you are using one of Apple's root view controllers (tab or navigation). The navigation is handled by the root view controllers and you are pretty much free to focus on your views.
On the ipad the split view and tab view controllers are not always useful, and for the app I'm working on they do not cut it. I have created separate navigation controller objects to handle hiding/show view controllers based on notifications that get posted when the user performs actions.
Anyone else have experience with solving the navigation problem on the iPad?
have you checked out MGSplitViewController by Matt Gemmell?
http://mattgemmell.com/2010/08/03/mgsplitviewcontroller-updated
I'm not claiming it'll solve all your navigation problems, but it's an interesting idea and may help you in finding more/better ways of handling view controllers.
With iOS 5 Apple has added the concept of Container View Controllers. This makes adding and removing children easy, allowing their methods for rotation/appear/disappear to be called automagically. Cool stuff!
Also, be careful using multiple view controllers for views which don't fill the full screen. Apple's documentation explicitly states that you shouldn't use view controllers for partial-screen views:
Note: You should not use view controllers to manage views that fill only a part of their window—that is, only part of the area defined by the application content rectangle. If you want to have an interface composed of several smaller views, embed them all in a single root view and manage that view with your view controller.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html
Although some things might work when you have multiple view controllers managing different sub-views, other things won't work. For example, only one of your view controllers will be informed when the device is rotated. Likewise, not all your view controllers will be sent 'viewWillAppear' 'viewDidUnload' etc messages that you might expect.

Resources