ios - 从 View Controller 访问变量到覆盖函数

标签 ios iphone swift xcode mobile

我正在制作一个应用程序,每次摇动手机时都会从数组中选择一个随机问题。如下所示;我试图在“覆盖函数motionBegan”中调用“打印”函数,但它不起作用。有人可以帮忙吗?

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;

    }

最佳答案

您可以使用以下代码开始打印随机值。您需要在运动开始时获取随机值。此外,您还需要将打印函数从 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() 
}

关于ios - 从 View Controller 访问变量到覆盖函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45584775/

相关文章:

ios - 在 Swift 中使用可达性

iphone - UINavigationBar 标题和右键

ios - 网页抓取 Facebook 的最佳方式是什么

json - 可解码的 JSONDecoder 处理相同值的不同编码键

iphone - 使用 iOS 进行 SMTP 发送的框架

ios - XCode 3.2.5 使用的 SVN 版本 - 不支持的工作副本格式

ios - 如何检测 UIStatusBar/iPhone 上的触摸

ios - iOS获取键盘语言

ios - 使用 Core Motion Framework 实现 UIDeviceOrientationFaceDown

swift - 如何在 SwiftUI 中动态隐藏导航后退按钮