Reset UI Label to default settings


#1

Hi ! I have a quick question. I have a Label in my app but later on in the program I change it’s settings such as TextColor, Shadow, etc. Then I want to change it back to default (how it was when app starts) so my question, is it possible to somehow set it to default (myLabel.default?) or only way is manually change each setting (color,text,shadow) to default?


#2

Every time you need to go back to something you need to created a reference to it, a mirror if you wish.
So you can create a second label at the start and have it be equal to the one you want to work with.

Lets say:

var myLabel: UILabel!

then after you define the starting values of your label you do:

let storingLabel = myLabel

then whenever you need to reset your main label you can do:

myLabel = storingLabel …


#3

Yes thanks actually I was trying to do something like this however I’m not getting the first few steps let’s say I have an outlet
@IBOutlet weak var attention1Label: UILabel!

and I need to store it in some var in order to get its initial value back after modifications
you have mentioned
var myLabel: UILabel! - but I don’t see a reference to a particular label just UILabel! in general

do I need to put a statement in ViewDidLoad? something like this var myLabel: UILabel! attention1Label ?


#4

No, you can just declare the storing Label and make it equal to attention1Label
since you have declared the label via the Story Board…


#5

I get an error “‘ViewController.Type’ does not have a member named myLabel”

my code is something like this:

@IBOutlet weak var myLabel: UILabel!

let storingLabel = myLabel


#6

Can you post the full code ?
I think that error is due to WHERE exactly things are declared.


#7

So here is the code: I press button1 and myLabel changes, next I press button2 and myLabel needs to get back to normal. I commented out the code in question.

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var myLabel: UILabel!

// let storingLabel = myLabel

@IBAction func Button(sender: AnyObject) {
    
    myLabel.textColor = UIColor.redColor()
    myLabel.text = "TEST"
    
    
}


@IBAction func Button2Pressed(sender: AnyObject) {

// myLabel == storingLabel

}





override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

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

}


#8

I might be able to run it later, but in general whenever you have an operation you need to have it within a method. The ‘=’ ( equal) operator does a calculation so to say, so it needs to be in a method of some sort.
You can either put it in a custom function or move it within the viewDidLoad() { } method.


#9

yes, I know it’s just to show a concept. The main question is how to store UILablel in a variable or constant. I tried many different options.


#10

Sorry, didn’t have time to run it last night, will try at lunch today.
Also do not use ‘==’ , that’s only to check if something is equal to something else , i.e. if a == b { then blabla } else { …

You store anything in an object of the same type.
So you can create an empty UILabel and then store in it.
But you should be able to create it on the spot with the let.
You only need to make sure a few things:

you already have the original Label present
the original Label is not empty ( nil )
you are doing the operation from within a method


#11

I guess I’ve been wrong, that doesn’t work the way I thought it would :frowning:

Unfortunately you would have to copy the parameters one by one…annoying right…
I thought I could use the global constants that Apple introduced with Swift 1.2, but it didn’t work either…

The only solution I can come up with that does not include hard manual labor :smiley:
is to use the NSKeyedArchiver…but that’s a more advanced method and it just seems as an overkill if you are just using it for a label :smile:

Someone else might know something better… @PaulSolt ?


#12

Hehe that’s what I thought but thanks a lot for trying . I guess I’ll stick with manual labor for now, it’s only extra 4 lines of code matter of fact is we wrote here already at least 10 times more text :floppy_disk:


#13

However I have another question about label text color. I’m using a custom color that I chose from colors picker. But after I make changes to my label, I change its color to some standard color (manually) and when I need to go back to the previous state how do I identify that color? Color picker doesn’t give me it’s number of anything… So what I can do is limited to system colors such as this:
myLabel.textColor = UIColor.redColor()


#14

Not sure if there is a way to get the color of UI Storyboard element…
what you can do is create another UILABEL and then give it the same color, but never add it to the Subview.
Then use it to get the color back from it.
Other way is to use a configurated color, instead of the standard UIColor, something like:

let myColor = UIColor(red: 0xFF, green: 0xA5, blue: 0)


#15

yes that makes sense also I think I can hide it and show another one


#16

@ravenshore @kc_dev I would create a data object to store all the default values.

If you know the values, you can hard code them in the new class, otherwise you can make them changeable by doing the archiving/unarchiving process – or you can do NSUserDefaults to save/load settings by string value.

I don’t think I’d would make placeholder UI elements to store values, you really just need the numeric or color data, not all the extra UI code.

struct ColorSettings { // Use a structure, not a class to store data
    var color = UIColor(red: 0.5, green: 0.5, blue: 1.0, alpha: 1.0)
    var selectedItem = 0
    var title = "Hello Colors"
}

class ColorViewController: UIViewController {
    var defaultColorSettings = ColorSettings()  // used to restore defaults if requested
    var currentColorSettings = ColorSettings()  // save and load to disk
    override func viewDidLoad() {
        
        // view logic
        
        // data setup
    }

    func resetDefaults() {
        currentColorSettings = defaultColorSettings
    }
}

The reason for the struct instead of a class is that you don’t want to edit the same object. classes (objects) are passed by reference, and if you were to set another variable to the same object, it doesn’t copy by default. Structures are passed by value, so all the data will be a copy, which makes sure that you can reset to the defaults and not modify them.


#17

Yeah, I was thinking of a similar solution with a struct or a separate class, subclass of UILabel, however I wish there was an easier method and I am kind of surprised there is none :smile:

I thought I would be able to do this at first:

declare a global constant at the start ,
then make it equal to the Storyboard UILabel and since it’s a constant, you won’t be able to
change it anymore, once given it’s value.
However that didn’t work and I kinda get why, but I wish that was not the case :smiley:


#18

@ravenshore making a constant UILabel doesn’t prevent you from making changes to the object itself.

let only prevents you from changing the object reference, but not the underlying data stored in the object. To prevent changes you’d want to use a struct.