swiftoutlet

Reduce number of Outlets? Swift


I've started learning Swift and started off with some simple Games. But every time (depending on the Game) my Code would look like this:

@IBOutlet weak var B1: UIButton!
@IBOutlet weak var B2: UIButton!
@IBOutlet weak var B3: UIButton!
@IBOutlet weak var B4: UIButton!
@IBOutlet weak var B5: UIButton!
@IBOutlet weak var B6: UIButton!
@IBOutlet weak var B7: UIButton!
@IBOutlet weak var B8: UIButton!
@IBOutlet weak var B9: UIButton!
@IBOutlet weak var B10: UIButton!
@IBOutlet weak var B11: UIButton!
@IBOutlet weak var B12: UIButton!
@IBOutlet weak var B13: UIButton!
@IBOutlet weak var B14: UIButton!
@IBOutlet weak var B15: UIButton!
@IBOutlet weak var B16: UIButton!
@IBOutlet weak var B17: UIButton!
@IBOutlet weak var B18: UIButton!
@IBOutlet weak var B19: UIButton!
@IBOutlet weak var B20: UIButton!
@IBOutlet weak var B21: UIButton!
@IBOutlet weak var B22: UIButton!
@IBOutlet weak var B23: UIButton!
@IBOutlet weak var B24: UIButton!
@IBOutlet weak var B25: UIButton!
@IBOutlet weak var B26: UIButton!
@IBOutlet weak var B27: UIButton!
@IBOutlet weak var B28: UIButton!
@IBOutlet weak var B29: UIButton!
@IBOutlet weak var B30: UIButton!
@IBOutlet weak var B31: UIButton!
@IBOutlet weak var B32: UIButton!

@IBOutlet weak var L1: UILabel!
@IBOutlet weak var L2: UILabel!
@IBOutlet weak var L3: UILabel!
@IBOutlet weak var L4: UILabel!
@IBOutlet weak var L5: UILabel!
@IBOutlet weak var L6: UILabel!
@IBOutlet weak var L7: UILabel!
@IBOutlet weak var L8: UILabel!
@IBOutlet weak var L9: UILabel!
@IBOutlet weak var L10: UILabel!
@IBOutlet weak var L11: UILabel!
@IBOutlet weak var L12: UILabel!
@IBOutlet weak var L13: UILabel!
@IBOutlet weak var L14: UILabel!
@IBOutlet weak var L15: UILabel!
@IBOutlet weak var L16: UILabel!
@IBOutlet weak var L17: UILabel!
@IBOutlet weak var L18: UILabel!
@IBOutlet weak var L19: UILabel!
@IBOutlet weak var L20: UILabel!
@IBOutlet weak var L21: UILabel!
@IBOutlet weak var L22: UILabel!
@IBOutlet weak var L23: UILabel!
@IBOutlet weak var L24: UILabel!
@IBOutlet weak var L25: UILabel!
@IBOutlet weak var L26: UILabel!
@IBOutlet weak var L27: UILabel!
@IBOutlet weak var L28: UILabel!
@IBOutlet weak var L29: UILabel!
@IBOutlet weak var L30: UILabel!
@IBOutlet weak var L31: UILabel!
@IBOutlet weak var L32: UILabel!

@IBOutlet weak var C1: UIButton!
@IBOutlet weak var C2: UIButton!
@IBOutlet weak var C3: UIButton!
@IBOutlet weak var C4: UIButton!
@IBOutlet weak var C5: UIButton!
@IBOutlet weak var C6: UIButton!
@IBOutlet weak var C7: UIButton!
@IBOutlet weak var C8: UIButton!

@IBOutlet weak var randomPin1: UIButton!
@IBOutlet weak var randomPin2: UIButton!
@IBOutlet weak var randomPin3: UIButton!
@IBOutlet weak var randomPin4: UIButton!

I am pretty unhappy with this, is there a way to use those Buttons (or whatever type it is) without creating an '@IBOutlet weak var name: Type!'? Like with an identifier and then using the identifier to access a Button? Or is that just the normal way to do this? Thanks in Advance!


Solution

  • You should use IB Outlet Collections and store these instance in arrays:

    http://nshipster.com/ibaction-iboutlet-iboutletcollection/ https://medium.com/@abhimuralidharan/what-is-an-iboutletcollection-in-ios-78cfbc4080a1