在textview上打印数组中的随机字符串

Printing random string from an array on textview

我似乎无法将我的数组打印到我的文本视图中。我有一个数组,并将我的 textview (texthold) 设置为数组的 randomIndex。你们看到我做错了什么吗?

这是我的按钮,单击它应该会从设备数组中获取一个随机字符串并将其打印到文本视图字段中。单击按钮时我没有收到任何错误,只是文本视图上没有任何显示

@IBAction func Generate(_ sender: Any) {
    let devices = ["Apple Watch", "iPhone", "iPad", "Mac", "Apple TV"]
    // Generate a random index
    let randomIndex = Int(arc4random_uniform(UInt32(devices.count)))
    // Get a random item
    let randomItem = devices[randomIndex]
    texthold.text = devices[randomIndex]

}

这段代码在我的程序中有效。使用 "Debug view hierarchy tool" 检查您的布局,也许会有帮助。
并且:

  1. texthold.text = randomItem
  2. 用小写字母命名方法(生成 -> 生成)
  3. 检查你的触摸按钮是否调用了这个方法
  4. 检查您的 UI 商品是否已连接插座。

从后台线程更改文本可能有问题,但如果您确实从 storyboard/xib 连接了此方法作为出口操作的方法 - 这种情况是不可能的。

并检查您的代码是否在按钮的 .touchUpInside 操作上被调用(情节提要中的第二个屏幕截图)!