6

I've read through both the iOS View Controller Programming Guide and iOS View Programming Guide and can't work out the best way to do the following without a lot of replication in my code or storyboard.

I'm working on a simple maths tutorial program for my children which has a View with a "NumberPad", digits 0-9, in it along with an SKScene for some animation.

So far I have one viewcontroller that handles the presentation of simple add, subtract, mulitply and divide problems which is working well.

I'd like to use the same number pad for a viewcontroller that will handle long divison and multi-digit multiplication and that's where I run into trouble trying work out how to reuse the NumberPad/SKScene view.

I'm pretty sure I can work out how to create this programatically from any number of examples on how to programatically switch from one View Controller to another. But I'd much rather use interface builder to achieve this given the ease with which I can layout the controls.

TJA
  • 2,969
  • 2
  • 25
  • 32
  • You can copy and paste views from one scene in a storyboard to another, but of course that creates 2 instances of that view. Are you trying to avoid that? You can create a single view (in a xib) or a single view controller (in a storyboard that you will add as a child controller) and instantiate it once, but add and remove it from various controllers as you move between them. I just answered a similar question here, http://stackoverflow.com/questions/23728986/storyboard-how-to-link-a-single-view-to-multiple-container-views/23741784#23741784 – rdelmar May 20 '14 at 02:14
  • Yes trying to avoid the cut and paste. – TJA May 20 '14 at 02:51
  • Also I was hoping to avoid the embedded viewcontroller approach. Maybe what I want to do just isn't storyboard friendly. Should I just bite the bullet, create a base class that handles the display of the NumPad and a protocol(I think) that defines the input handling methods that need to be overridden. – TJA May 20 '14 at 02:54
  • No, I don't think it is very storyboard friendly unless you want to use copy an paste. Unless you're talking about a large number of controllers, what's wrong with copy and paste? – rdelmar May 20 '14 at 03:27
  • If I take the app as far as I think I might then we are talking about a dozen different ViewControllers that could use the NumPad. I guess I'm a bit reluctant to let go of the ease of design which InterfaceBuilder gives me. I also rather like the visual self-documenting nature of storyboards, but the assembler programmer I once was dislikes replication. – TJA May 20 '14 at 03:51

1 Answers1

2

Have you ever considered ContainerView? ContainerView is suitable to create common view for different viewcontrollers in your project. ContainerView is very powerful if you know how to use it. I have been using quite a lot in my project once I know how to use it.

Yesterday, there was an almost similar question about "common UITableView for all view controllers?", you may see my answer there:-

How to create common UITableView for all view controllers?

This link might help too: How to use a 'Container View' in iOS?

Community
  • 1
  • 1
Ricky
  • 10,485
  • 6
  • 36
  • 49
  • Ok that might well do the trick. Suspect the idea didn't occur to me is because other containers like the Navigation and Tab ViewControllers only pinch a little bit of real estate at the top or bottom of the screen whereas my NumberPad pushes way up into the view especially in landscape mode. – TJA May 20 '14 at 03:38
  • You may trying that out together with the AutoLayout Constraint. With the correct settings, I believe it will look good in both portrait and landscape. – Ricky May 20 '14 at 03:43
  • Thanks. Got some reading to do. Will be starting with https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html – TJA May 20 '14 at 03:58
  • No problem. If I answer your question correctly, don't forget to upvote and accept as answer. Thanks. – Ricky May 20 '14 at 04:00
  • I suspect I'm going to wind up with a combination of storyboard and programmatic transitions. I'll need to go and have a play with this. – TJA May 20 '14 at 04:12
  • @TJA, I thought you said you didn't want to use embedded view controllers. What do you think container views give you? – rdelmar May 20 '14 at 04:13
  • @rdelmar I have to apologise for misinterpreting what an embedded view controller is. I followed the links you and Ricky provided and it looks like I may use a ContainerView in my ViewController but link it to the desired ViewController programmatically. Now the you've pointed me to some ideas its time to start experimenting with them. – TJA May 21 '14 at 07:19