2

I can't seem to get my array printed onto my textview. I have an array and have set my textview (texthold) to the randomIndex of the array.. Anything you guys see I'm doing wrong?

This is my button that when clicked is supposed to get a random string from the devices array and print it into the textview field. I am getting no errors just nothing displays onto the textview when the button is clicked

@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]

}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • 3
    Did you debug your code? Try placing a break point in there and check if it's really hit. Did you connect the outlet of your UITextView? – Hannes Feb 19 '18 at 16:34
  • This code works in my program. Check your layout using "Debug view hierarchy tool", maybe it will help. And texthold.text = randomItem – Sergey Fedorov Feb 19 '18 at 16:52

1 Answers1

0

This code works in my program. Check your layout using "Debug view hierarchy tool", maybe it will help.
And:

  1. texthold.text = randomItem
  2. Name methods with lowercase (Generate -> generate)
  3. Check if your button touch calls this method
  4. Check if your UI items have connected outlets.

There may be problem with changing text from background thread, but if you really have this method connected from storyboard/xib as method for outlet's action - this situation impossible.

And check that your code is being called on .touchUpInside action of your button (second screenshot in storyboard)! Code example

Connected outlets example

Debug view hierarchy tool place

Sergey Fedorov
  • 199
  • 1
  • 6