0

I have multiple storyboards in my project, each having an almost identical view controller (only dimensions are different). The storyboard used depends on the device in use. What I want to do is have the equivalent element from each storyboard under the same IBOutlet. This way, whatever I do to an element in the storyboard being used, the same would be done for all other storyboards. This is instead of creating an IBOutlet for the same element in each different storyboard.

For example, we may have two buttons, one in each storyboard. They are meant to be the same button but in different sizes, I set this button's alpha to 0 at one point the in Swift file. How could I do this for both buttons under one name (the same IBOutlet)? I know this means doing something on a storyboard which isn't even being used and therefore not accessible on the device, and I'm not sure whether it'll spit up an error or not. Surely this is a way around this though, because there are apps which use multiple storyboards.

I could imagine possibly stating if (storyboard == xnamex) {execute code for specific storyboard}, but this would mean having multiple if statement with the whole code repeated for different storyboards, and having to create an IBOutlet for each element, which is unrealistic. How would I get around doing this?

Many thanks.

4u53r
  • 717
  • 1
  • 7
  • 17
  • You can configure the alpha and the size of the button on each storyboard file. or you have to do it dynamically? – Jože Ws May 26 '17 at 17:25
  • Is there a reason you don't just use size classes? It sounds like it's exactly what you need. – GetSwifty May 26 '17 at 18:18
  • Easiest way to find out is to try it - I just did (spoiler alert: works fine). But... yes, the real question is "why don't you use auto-layout and size classes"? – DonMag May 26 '17 at 18:43
  • @JožeWs It is done programmatically in Swift. – 4u53r May 26 '17 at 22:16
  • @PEEJWEEJ Using Storyboard for each device, more convenient for this app. – 4u53r May 26 '17 at 22:16
  • @DonMag What did you try that worked? – 4u53r May 26 '17 at 22:17
  • @4u53r - I created a new project... added 2 new Storyboards... added a UIViewController to each... added a UILabel to each View Controller... added a new class `MyViewController`... added an IBOutlet for a UILabel in that class... assigned that class to the View Controller in each Storyboard... connected the Labels to the IBOutlet... added a line in `viewDidLoad()` -- theLabel.text = "This works"... ran the app with SB2... ran the app with SB3... worked both times, with the label text showing up as desired. – DonMag May 26 '17 at 22:57
  • @4u53r It might seem "easier", but it's a bad approach that severely limits your possibilities. e.g. what do you do when iPad re-sizes? how do you support extra interface on iPhone+? You'll have to manually handle each one of these cases. Using size classes to edit/add/remove elements and constraints is the almost always better than 2 separate storyboards or scenes. – GetSwifty May 27 '17 at 08:08

1 Answers1

0

If it's exactly the same button except as you mention the size on it. You can just pull an outlet to the same name and they both will be contained in there. As mentioned [Multiple buttons connected buttons best practise you pull the outlets to the same place and then action to the same place as well. However. Sometimes the action can be tricky, if you get problem there, just create a new action with the exact same name and then remove it. It will still be connected to the same name.

Vollan
  • 1,887
  • 11
  • 26