2017-08-09 106 views
0

我正在製作一個應用程序,每次我搖動我的手機時都會從我的陣列中選擇一個隨機問題。如下所示;我試圖在「覆蓋函數motionBegan」中調用「print」函數,但它不起作用。任何人都可以幫忙。?從視圖控制器訪問變量覆蓋函數

override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     let array = ["what did your eat for breakfast?", "First time you kissed a boy/girl? Explain.", "Whats your body count?", "When was the last time you went to church?", "Would you date someone from another religion?", "East booty(Guy) ?or let a guy eat your booty(Girl)?", "If you die right now where will you end?", "whats on your mind?", "Ever has a one night stand?", "Do you find the other person attractive?", "How old where you when you first hd sex?. Explain.", "You believe in Jesus?", "Where do you see yourself in 10 years?", "First time you gave head?", "Slap your mum or your dad? Choose one.", "Last time you watched porn?", "How much is in your account?", "One thing you hate about the other person?", "One thing you love about the other person?", "Would you kiss the person close to you?", "One thing you hate about yourself?", " One thing you love about yourself?","Suck toes or eat booty?", "Best rapper?", "Best singer?", "If you had a billion dollars for 24 hours how will you spend it?"] 

      let randomIndex = Int(arc4random_uniform(UInt32(array.count))) 
      // Get a random item 
      let randomItem = array[randomIndex] 

     func print() { 
      textField.text = randomItem 
     } 
    } 

    override func motionBegan(_ motion: UIEventSubtype, with event: UIEvent?) { 
     print; 

    } 
+0

什麼問題?在執行'motionBegan'函數時你需要'覆蓋'嗎? –

+0

您在Didload中選擇了動態問題,而不是在創建的函數中選擇它,並在檢測到的動作結束時調用新創建的函數,並在didLoad中調用一次,希望它有幫助 –

+0

您確定代碼格式良好嗎?你的方法之前有太多的閉括號? – Maxime

回答

0

您可以使用下面的代碼開始打印隨機值。運動開始時,您需要獲取隨機值。此外,您需要將打印功能從viewDidLoad中移出,以便每次都可以調用它。

let array = ["what did your eat for breakfast?", "First time you kissed a boy/girl? Explain.", "Whats your body count?", "When was the last time you went to church?", "Would you date someone from another religion?", "East booty(Guy) ?or let a guy eat your booty(Girl)?", "If you die right now where will you end?", "whats on your mind?", "Ever has a one night stand?", "Do you find the other person attractive?", "How old where you when you first hd sex?. Explain.", "You believe in Jesus?", "Where do you see yourself in 10 years?", "First time you gave head?", "Slap your mum or your dad? Choose one.", "Last time you watched porn?", "How much is in your account?", "One thing you hate about the other person?", "One thing you love about the other person?", "Would you kiss the person close to you?", "One thing you hate about yourself?", " One thing you love about yourself?","Suck toes or eat booty?", "Best rapper?", "Best singer?", "If you had a billion dollars for 24 hours how will you spend it?"] 
var randomItem = "" 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 

func printing() { 
    let randomIndex = Int(arc4random_uniform(UInt32(array.count))) 
    randomItem = array[randomIndex]   
    textField.text = randomItem 
} 

override func motionBegan(_ motion: UIEventSubtype, with event: UIEvent?) { 
    self.printing() 
} 
+0

This Worked !!!!謝啦! – Perewillz

+0

你有沒有想過如何防止應用程序詢問相同的問題,直到我已經詢問了陣列中的所有問題?你認爲我可以從數組中刪除問題項每次我問它,直到數組== 0? – Perewillz

+0

嗨,謝謝,是的。你需要做的是創建另一個數組,通過從列表中隨機獲取元素來填充另一個數組,並從另一個列表中刪除這些元素。一旦你有了這個數組,你可以從頭開始,並且不需要隨機地從它中取出對象。 –

相關問題