2015-12-21 75 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