Search code examples
iosarraysswiftif-statementiboutlet

Re-write some basic swift code, with big if-statements


I am working on a project, with a UITabBarcontroller, in Xcode using swift code.

In this project the users can (among other stuff) choose their favorite images. These favorite images will be set on 30 buttons I have on my favorites UIViewController. To accomplish what I want, I developed some very basic code: I assigned 30 IBOutlets for the 30 buttons and I made 30 (big) if-statements. It in fact works, but I know this code can be done in a simpler and more concise manner. And I am not yet able to do that.

I really want to learn, so can anybody help me with a way to rewrite this code? Help is much appreciated :). Just a push in the right direction would already be great

Should I for example assign tag values to the 30 buttons, and ‘find’ the appropriate buttons with .viewWithTag (instead of the 30 IBOutlets). And should I for example use some sort of loop for dealing with the different counts of the array? (see below)

Here is my code:

// I have created a subclass of UITabBarController 
class TabBarData: UITabBarController {

/* In this class I have initialized an empty array to share between the various tabs. 
The array will be populated with the favorites chosen by the user. */

var array = [Int]()

}

/* There are multiple ViewControllers in my project that have the same 
code for adding a favorite. So for now I describe one example ViewController */

class exampleVC: UIViewController {

/* A variable that identifies the image by the number. There a a few 
hundred images in the project, every images has its own identifying number */
var imageNumber = 0

// This function will execute if user adds a favorite:
func userAddedFavorite(imageNumber: Int) {

// The ViewController within the TabBarController getting acces to the properties 
if let tbc = self.tabBarController as? TabBarData {

// The Array can not be bigger then a count of 30:
if tbc.array.count < 30 {

// When a user adds a new favorite image, the array gets filled with a new value:

tbc.array.append(imageNumber)


// Now I  set the button images, in viewWillAppear, for my favorites VC:

class Favorites: UIViewController {

// IBOutlets for the 30 buttons
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!

// etcetera…

 override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

 if let tbc = self.tabBarController as? TabBarData {

if tbc.array.isEmpty {

    print("The user has no Favorites at the moment. The array is empty")

    } else if tbc.array.count == 1 {

   // At the moment the images in my project have been named: test1/test2/test3 etc...   
    button1.setImage(UIImage(named: "test\(tbc.array[0])"), forState:  .Normal)

                } else if tbc.array.count == 2 {

                    button1.setImage(UIImage(named: "test\(tbc.array[0])"), forState: .Normal)
                    button2.setImage(UIImage(named: "test\(tbc.array[1])"), forState: .Normal)

                } else if tbc.array.count == 3 {

                    button1.setImage(UIImage(named: "test\(tbc.array[0])"), forState: .Normal)
                    button2.setImage(UIImage(named: "test\(tbc.array[1])"), forState: .Normal)
                    button3.setImage(UIImage(named: "test\(tbc.array[2])"), forState: .Normal)

                } else {

                    print("etcetera.....,the if-statements getting bigger each count up......")
                }

Solution

  • Instead of making 30 IBOutlets (one for each button,) make a single IBOutletCollection that holds all thirty:

    @IBOutlet var buttons: [UIButton]!
    

    Then you can:

    for (index, item) in tbc.array.enumerate() {
        buttons[index].setImage(UIImage(named: "test\(item)"), forState: .Normal)
    }