2016-10-17 72 views
-2

我有一個要求來構建一個多選Q &使用Swift的iOS應用程序。目前我正在使用如下所示的「案例」構造。我面臨着兩個問題:Swift構造多選

  1. 我不知道如何刪除的問題(或情況),一旦它已經被正確
  2. 這種結構就變得很麻煩,當我建立的問題,數百或數千回答。

有人可以提供一個更好的方式來實現這個?

func RandomQuestions() { 
    var RandomNumber = arc4random() % 4 

    switch (RandomNumber) { 

    case 1: 
     QuestionLabel.text = "What is the capital of Austria? " 
     Button1.setTitle ("Istanbul", for: UIControlState.normal) 
     Button2.setTitle ("Vienna", for: UIControlState.normal) 
     Button3.setTitle ("Melbourne", for: UIControlState.normal) 
     Button4.setTitle ("Perth", for: UIControlState.normal) 
     CorrectAnswer = "2" 

     break 
    case 2: 
     QuestionLabel.text = "What is 6 x 7? " 
     Button1.setTitle ("12", for: UIControlState.normal) 
     Button2.setTitle ("24", for: UIControlState.normal) 
     Button3.setTitle ("42", for: UIControlState.normal) 
     Button4.setTitle ("48", for: UIControlState.normal) 
     CorrectAnswer = "3" 

     break 
    case 3: 
     QuestionLabel.text = "What is the name of the yellow Power Ranger? " 
     Button1.setTitle ("Willy", for: UIControlState.normal) 
     Button2.setTitle ("Mario", for: UIControlState.normal) 
     Button3.setTitle ("Ivan", for: UIControlState.normal) 
     Button4.setTitle ("Alexander", for: UIControlState.normal) 
     CorrectAnswer = "3" 

     break 
    case 4: 
     QuestionLabel.text = "How many dwarves in Cinderella" 
     Button1.setTitle ("12", for: UIControlState.normal) 
     Button2.setTitle ("7", for: UIControlState.normal) 
     Button3.setTitle ("3", for: UIControlState.normal) 
     Button4.setTitle ("9", for : UIControlState.normal) 
     CorrectAnswer = "2" 

     break 

    default: 
     break 
+0

請不要硬編碼您的所有問題並在代碼中回答。把它放在plist文件或數據庫或其他數據存儲中。在應用運行時加載問題。 – rmaddy

回答

1

我會做的是設計一個簡單的模型對象(一個結構),其屬性是文本,四個按鈕標題和正確的答案。稱它爲問題。現在你的未回答的問題列表成爲一系列問題。隨機數是該數組的索引。此外,該數組可以是可變的,所以你可以從中刪除正確回答的問題。