Re-write 一些基本 swift 代码,大 if-statements

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

我正在 Xcode 中使用 swift 代码开发一个 UITabBarcontroller 项目。

在这个项目中,用户可以(除其他外)选择他们喜欢的图像。这些最喜欢的图像将设置在我收藏夹 UIViewController 上的 30 个按钮上。 为了完成我想要的,我开发了一些非常基本的代码:我为 30 个按钮分配了 30 IBOutlets,我制作了 30(大)if-statements。它实际上有效,但我知道这段代码可以用更简单、更简洁的方式完成。我还不能那样做。

我很想学习,所以谁能帮我重写这段代码?非常感谢您的帮助 :)。只要朝着正确的方向推动就已经很棒了

例如,我是否应该将标签值分配给 30 个按钮,并使用 .viewWithTag(而不是 30 个 IBOutlets)“查找”合适的按钮。我是否应该使用某种循环来处理数组的不同计数? (见下文)

这是我的代码:

// 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......")
                }

与其制作 30 个 IBOutlet(每个按钮一个),不如制作一个包含全部 30 个的 IBOutletCollection:

@IBOutlet var buttons: [UIButton]!

那么您可以:

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