0

I am writing an application with a customizable color palette. I have a settings class (settings) which stores these colors, and a settings viewcontroller (themeVC) which lets the user modify them, and another to list the possible values (colorsVC).

I keep a reference to the settings in both view controllers:

let settings = Settings.sharedInstance

In the themeVC I list categories like background, text and so on in a tableview like this:

let cell = tableView.dequeueReusableCellWithIdentifier("right detail with disclosure", forIndexPath: indexPath) as UITableViewCell

switch indexPath.row {

                case 0:
                    cell.textLabel?.text = "Backgrounds"
                    cell.detailTextLabel?.text = settings.backgroundColor.name()

                case 1:

                    ....

                default:
                    cell.textLabel?.text = "Texts"
                    cell.detailTextLabel?.text = settings.textColor.name()
                }
return cell

I am trying to pass the settings value to the colorsVC, but this way it is just copied:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if (segue.identifier == "showColorsViewController") {
            let detailVC = segue.destinationViewController as! ColorsViewController

            detailVC.setting = settings.backgroundColor
        }
    }

ColorsVC lets the user pick a color and then modifies the corresponding value in settings based on which row was selected in themeVC.

How can I pass a different value for every cell so the receiving colorsVC can read AND modify the corresponding setting?

I am looking for something like the inout keyword, but sadly I couldn't use it in this scenario.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
xanyi
  • 127
  • 1
  • 10

1 Answers1

0

Classes are reference types:

Unlike value types, reference types are not copied when they are assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used instead.

In the case above I think you would be best setting detailVC.setting to setting and then accessing/modifying the object properties on the object directly instead of passing the value of a class property, Does this help?

See Classes are Reference Types in the documentation

EDIT:

I believe you are using the colorsVC to set a color on the themeVC, so you segue to the colorsVC, select a color and this is returned to the themeVC table cell, correct?

If so there are a couple of ways to achieve this, I think the best way would be to pass the cell's row to the colorsVC so that it can update it as needed.

Scriptable
  • 19,402
  • 5
  • 56
  • 72
  • I need to know which color to modify in the colorsVC. Is there a way to pass the color as a reference? – xanyi Oct 10 '15 at 12:37
  • I dont think you can by reference, you'd be best passing the settings object to the detailVC and then using self.setting.backgroundColor = UIColor.blackColor() or something similar, then it will update. Otherwise you'd likely need to keep a single instance of the object centrally, maybe in the AppDelegate or in NSUserDefaults – Scriptable Oct 10 '15 at 12:39
  • It is not just the background, I have a lot more to modify. Is there a way to know which color without passing a number as id and creating a ton of switches in the colorsVC? – xanyi Oct 10 '15 at 12:43
  • I dont fully understand what you mean, what exactly does your colorsVC do? are you just using colors to select a color and you expect that to pass a value back to the themeVC? – Scriptable Oct 10 '15 at 12:45
  • colorVC needs to modify the settings. Also updated the question – xanyi Oct 10 '15 at 12:52
  • "all references to the object are updated" isn't the right way to phrase this and it's certainly not the right way to think about it. What does it mean for something declared with `let` which should be constant to be "updated"? To understand what's happening, you need a proper understanding of stack versus heap... – nhgrif Oct 10 '15 at 12:52
  • @nhgrif I've updated the answer with a quote from the documentation that explains a little about reference types, I do understand the difference between the stack and the heap but I do not see the benefit of explaining that here. Maybe I could of explained better, but I did answer the question. Thanks for pointing that out – Scriptable Oct 10 '15 at 13:04
  • I'm not suggesting your answer contain the full explanation. I'm only suggesting that your answer not use misleading/confusing wording, and perhaps for bonus points, point to some SO answer or documentation of some sort that describes more deeply what is going on, for anyone who may be interested. – nhgrif Oct 10 '15 at 13:09