重写一些基本的swift代码,使用大的if语句

时间:2015-12-21 15:05:56

标签: ios arrays swift if-statement iboutlet

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

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

我真的想学习,所以有人可以帮我改写这段代码吗?非常感谢帮助:)。只是推动正确的方向已经很好了

例如,我应该为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......")
                }

1 个答案:

答案 0 :(得分:5)

不是制作30个IBOutlets(每个按钮一个),而是制作一个包含所有三十个的IBOutletCollection:

@IBOutlet var buttons: [UIButton]!

然后你可以:

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