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
Related
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.
In my app I have a sort of "dashboard" with buttons, images, labels, etc.
This dashboard is common, apart from small variations, to several view controllers.
I thought that a neat solution would be to implement the dashboard as a separate view controller that is included by means of embed segues and view containers in all the view controllers that need it.
This works perfectly.
However, I have many view controllers in my storyboard, and most of them are connected to the dashboard view controller, which makes the storyboard completely unreadable (or, at least, the connections in it).
Is it possible then to create all these connections programmatically (so that the connections go away from the SB)? It seems to be possible to create segues programmatically, but what about embed segues?
If this is not a viable solution, what are the alternatives?
I'm interested in building a slide out sidebar for a view controller, and I've done a lot of reading on how to implement it. This tutorial, as well as virtually all the others go over creating child view controllers, which seems like a great way to go about it. However, it's always done in code for examples.
This post goes over how to create container view controllers in the Storyboard, but it left me confused how exactly container views work in the Storyboard, and there seems to be little in the way of explanations online.
Finally, this post went over how to implement a slide out menu with container view controllers and a Storyboard. Sounds perfect right? Well, in reading it I was confused how the author seems to use multiple container views. I thought there was supposed to be one that encompassed all of the subviewcontrollers. Is it implemented correctly in that post?
Basically, I'd love an explanation on how to implement multiple view controllers using container view controllers via the Storyboard. It's my understanding from the first tutorial that you have the view controller that contains everything, and then add view controllers to that container and then they all work together via delegates. Achieving this in Storyboards is leaving me scratching my head.
Adding a container basically adds a child view controller and its view to the main controller. I think you are not clear on two things here. First, each container can be linked to only 1 controller/view pair. So, if you want to have more than one child controllers, you must have more than one containers. Secondly, communication between parent and child controllers is handled by 'embed segues', not by delegates.
I am exploring the design options for an iPAD application with multiple views on the different part of the main screen. I am going to have different ViewController for each of the view. UI is quite different from what any of the available view controllers (UISplitViewController, UINavigationController etc.) provide. I have been reading about the container extension api of UIViewController (especially addChildViewController):
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIViewController_Class/Reference/Reference.html
(Look for "Implementing a Container View Controller")
However, it seems to me that this functionality is mainly designed for applications with UI that transition from one view to another (transitionFromViewController...). In my case, all of the views are visible at the same time. However, they do interact with each other. So my questions:
Am I missing something w.r.t. container extension of View Controller? I will still probably end up using this just to keep the list of child view controllers but don't see much value.
Can you recommend any other api/pattern that I should be using?
Thank you.
You can easy access to childViewController.
self.childViewControllers will return an array with all childs.
I asked similar question couple days ago: Maybe this will help you:
Get container VC instead of view
Take a look at http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html.
Add your various view controllers to self.childViewControllers and add the views of the child controllers as subviews of self.view.
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.