2015-12-21 73 views
1

我正在使用swift代码在Xcode中使用UITabBarcontroller进行项目工作。用大的if语句重写一些基本的swift代码

在这个项目中,用户可以(其他的东西)选择他们最喜欢的图像。这些最喜欢的图像将被设置在我最喜欢的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......") 
       } 
+0

你也应该看看[Code Review Stack Exchange](http://codereview.stackexchange.com/)。 –

+0

@DanielStorm是的,现在我明白了。这个问题是那个 – codeDude

回答

5

而不是让30个IBOutlets的(每个按钮),使一个单一的IBOutletCollection保存所有的30个:

@IBOutlet var buttons: [UIButton]! 

然后你可以:

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

艾艾的一个很好的候选人,这是简单的呃;)?你只要看一遍我的代码,然后拿出正确的答案。尼斯。我有很多东西要学习...... – codeDude

+1

@codeDude这里最大的障碍是你应该总是怀疑重复的编程。 –

+0

@DanBeaulieu是的,你绝对是对的。 – codeDude