23

In iOS5 using storyboard feature I want to create a custom container which will have 2 ViewControllers embedded in it. For Example, embed Table view controller as well as a view controller both in one ViewController.

That is, one view controller will have 2 relationship:

  1. to table view controller
  2. to view controller which in turn will have 4 UIImage view Or UIButton in it

Is creating this type of relationship possible using storyboard's drag drop feature only & not programmatically?

Soumya Ranjan
  • 4,817
  • 2
  • 26
  • 51
Sayali
  • 593
  • 3
  • 9
  • 22
  • I got a problem with your example. The CustomViewController viewDidLoad event is never fired… Is it normal ? Is there a way to fix that ? Otherwise, this is a really good post ! Thank you. – Clement M Dec 08 '11 at 17:18

2 Answers2

14

,You should only have one view controller to control the scene. However, this viewController might have two other view controllers that control particular subviews on your scene. To do this you create properties in your scene viewController, in your case one for your tableViewController and one for your view. I like to keep things together so I make both these viewControllers outlets and create them in interface builder. To create them in interface builder pull in an Object from the Object library and set its type to the relevant viewController. Hook it up to the appropriate outlet you just created in your scene's viewController - Note: this is important otherwise the viewController will be released if you are using ARC and crash your app. Then hook these viewControllers up to the view you want them to control and you are done.

Alternatively you can instantiate and hop up your viewControllers in your scenes viewController should you prefer to do this.

Hope this helps.

Edit: On reflection this is not a good idea and actually goes against the HIG you should maintain only one ViewController for each screen of content and instead try to create a suitable view class and have the single view controller deal with the interactions between the various views.

Scott Sherwood
  • 3,108
  • 25
  • 30
  • 3
    We can create outlet for views but how can one create outlet to viewController in storyboard? – Sayali Nov 15 '11 at 11:05
  • 2
    Open up interface builder and select your storyboard. On the bottom right you will see the object library (where you pull view controllers and views from) type object into the search box and you will see the little yellow/orange object drag that onto your scene and change its type in the identity inspector to your view controller. Then hook up a view that is on your scene for it to control and thats it. In your code just have an IBOutlet for your view controller class and hook up the object to that. – Scott Sherwood Nov 15 '11 at 11:19
  • But how to hook up a view to control? What i did is as follow:select storyboard file-> drag viewcontroller(Main view Controller), TableviewController(one of the sub view controller) & 1 more UIViewController (another sub view controller). Then in Main view controller I created 2 IBOutlets for UITableViewController & UIViewController. Then select Main UIViewController-> right click on it & connect the IBOutlet created to TableViewController in storyBoard. But it didn't create the connection. Do let me know if I am doing something wrong – Sayali Nov 15 '11 at 13:13
  • 2
    I have put together an example of how to do it you can download the source here, http://www.scott-sherwood.com/?attachment_id=564 hope this helps – Scott Sherwood Nov 15 '11 at 20:19
  • I have updated the sample code to use a tableView for you hope this helps. – Scott Sherwood Nov 16 '11 at 08:47
  • And tableView's delegate method didSelectRowAtIndexPath is not called after selecting row. Can you please help? – Sayali Nov 16 '11 at 13:40
  • I needed to hook up one of the delegates for handling the didSelectRow it should be sorted now. I think from the sounds of it though that this example answers your original question?! – Scott Sherwood Nov 16 '11 at 14:36
  • 4
    Scott, about your edit: UIViewConroller Containment was added in iOS 5 for just this purpose (allowing multiple controllers in a single "scene"). I just seems that documentation and examples are light, extremely basic, or non-existent. Your example is a great start. Any thoughts on how to put a UINavigationController in place of the UITableView? – sean woodward Jan 03 '12 at 17:23
  • @ScottSherwood, I tried out your sample and think I've done everything required (drag out an object in IB, mark its class as my custom viewcontroller, hook it up to a UIView on the screen, create variables of the custom viewcontroller within the present screen's controller and hook them up to the object). However, the controls from my custom viewcontroller's view (3 buttons, a text view, and an image view within it) do not show up; the connected view on the presenting screen is blank. Is your solution insufficient for presenting the controls from the custom viewcontroller? – Danny Mar 09 '13 at 07:13
0

There is a way to do it that isn't too hacky. It is described at the following URL for UITabBarControllers, which you could use the first view controller in the list control the first subview, and the second one control the other. Or, you can probably adapt the code to work with UISplitViewController.

http://bartlettpublishing.com/site/bartpub/blog/3/entry/351

Basically, it works by replacing the tabbarcontroller at runtime after iOS has finished configuring it.

johnnyb
  • 622
  • 5
  • 17
  • FYI - starting in iOS 7, this doesn't work anymore. There might be a way to get something similar going, but we switched our project to the standard tab bars. – johnnyb Jun 25 '15 at 16:23