2017-08-09 78 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

嗨,谢谢,是的。你需要做的是创建另一个数组,通过从列表中随机获取元素来填充另一个数组,并从另一个列表中删除这些元素。一旦你有了这个数组,你可以从头开始,并且不需要随机地从它中取出对象。 –

相关问题