The whole functions is:
Thanks for showing the code, things get clearer with it.
General advise for using the dev forums, you should better include tags
Swift and
UIKit, when you ask some programming issue about an iOS app using UIKit and Swift. (The tag
Xcode is not needed unless you want to ask how to use Xcode itself.)
And better use the
Code block feature of this site, shown as
< > in the tool bar of the editing area.
And one note about coding convention of Swift, in Swift, only type names start with Capital letter, your method
Random or your local variable
Player looks very odd for Swift programmers and takes more time to read it.
And one of the most important things in iOS programming,
(Never use any other
blocking functions in the main thread.)
Actual UI updates begin after the action method is finished. So your code just delays the UI updates and shows the result of the last thing you have done in the method.
You may need to work with
Timer or the background threads.
An example:
Code Block | class ViewController: UIViewController { |
| |
| @IBOutlet weak var label: UILabel! |
| |
| //... |
| |
| var prev = -1 |
| @IBAction func random(_ sender: Any) { |
| let places = ["Burger King","Chick-Fil-A","McDonald's","Wendys","Checkers","TacoBell","PDQ","Pizza Hut","Jimmy John's","Subway","Dominos","Pollo Tropical","Chipotle","Olive Garden","Ale House","Five Guys","Fresh Kitchen","Blaze","Outback"] |
| |
| label.isHidden = false |
| var i = 0 |
| var number = Int.random(in: 0..<19) |
| |
| if prev == number { |
| number = Int.random(in: 0..<19) |
| } |
| let now = Date() |
| let timer = Timer(fire: now, interval: 0.2, repeats: true) {timer in |
| if i < places.count { |
| self.label.text = places[i] |
| i += 1 |
| } else { |
| self.label.text = places[number] |
| self.prev = number |
| timer.invalidate() |
| } |
| } |
| RunLoop.main.add(timer, forMode: .default) |
| } |
| } |