Alia.RK
Alia.RK

Reputation: 23

How do you Change the UILabel.text property to iterate through a array of strings and not hardcode an index

I have been trying to figure this out for days. I have searched everywhere its very simple but I'm so confused.

import UIKit

class ViewController: UIViewController {

var arrays = ["A", "b", "C", "D", "E", "F"]

@IBOutlet weak var ChangeText: UILabel!

    override func viewDidLoad() {
         super.viewDidLoad()

         // Do any additional setup after loading the view, typically from a nib.

         ChangeText.text = arrays[0]
    }

    override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
    }


    @IBAction func Button() {
        ChangeText.text = arrays[1]
    } 


}

How do you change the text property which is an optional string in a xcode project to iterate through your array of strings without hardcoding the index of the array.

What I want is for the text in the label to change every time I press the button however given the value is Hardcoded it changes once and then that's it.

I've tried a for in loop and my own function, however I keep on getting errors and I don't know where to place my function or loop. So I've gotten rid of it.

Thank you for your patience and help

Upvotes: 0

Views: 494

Answers (3)

Stephen J
Stephen J

Reputation: 2397

Your question appears to have multiple aspects to it. The loop and not using an index in the answer is a matter of Scope. Variable Scope is global, local, or static. That is a matter of where they exist and when they stop existing. (there are more variable types but I don't think we should go into those here).

Now, the "other" aspect isn't in traditional programming. You're wondering why you can loop through everything but only one shows up. This is basic render-loop. It's for games, graphics, but not taught in language tutorials.

The program runs its code, all of it, and at the end it displays something on screen. So if you loop through everything, like this: for(string s in array) { textArea.text = s; } it will finish and always display the last one. Because it finished that loop before it rendered. What you want is to not change it until it's already visible.

iOS is basically a game loop. Or a graphics rendering loop. You don't have to worry about it, and you'll never see it. But if you want to know how to work with it, the Views Programming Guide, Animations Programming Guide, and Core Graphics Programming Guide will teach you how they work. At this stage though, I think you should focus more on the core language features.

I hope you've gotten some progress though. And always feel free to print things to the screen to learn

Upvotes: 0

Munahil
Munahil

Reputation: 2419

Set a index variable on top, and give it a value 0.

var index: Int = 0

Inside your func Button() function, increment count like this :

index = index + 1

and then :

if count<arrays.index
{
         ChangeText.text = arrays[index]
}

Upvotes: 0

Just store the currentIndex as a property

import UIKit

class ViewController: UIViewController {

var arrays = ["A", "b", "C", "D", "E", "F"]
var currentIndex = 0

@IBOutlet weak var ChangeText: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    ChangeText.text = arrays[currentIndex]
}

@IBAction func Button() {
    currentIndex++
    if (currentIndex == arrays.count {
        currentIndex = 0
    }

    ChangeText.text = arrays[currentIndex]
}
}

Upvotes: 1

Related Questions