Xcode 6 OS X storyboard multiple User Defaults Controllers bug with multiple scenes?
Asked Answered
R

2

5

Maybe I'm just doing something wrong here but I've ran into an issue that I can't find a solution for. Maybe someone has seen this?

Using Xcode 6, I create a new project with Objective C & Storyboards.

In the interface builder view, I place a text field on the view that comes with the storyboard and bind it to the 'Shared User Defaults Controller'. Everything works fine.

I add a second view controller. This creates a new scene. I place a text field on this view and bind it to the 'Shared User Defaults Controller' and here's the issue:

A 'User Defaults Controller' gets created in the scene. If I expand the dropdown list for 'Value' 'Bind to', there are now 2 'User Defaults Controller' objects in the list. The binding doesn't seem to work as the value doesn't get stored in the preferences file.

Did I do this wrong? Or is it a bug in OS X storyboards on Xcode 6?

Reunionist answered 28/3, 2015 at 0:10 Comment(1)
Confirmed this is still present on XCode 11.5Bellman
C
4

Yes, it is a bug in Xcode 6 with Storyboards. I found a workaround: edit the storyboard manually.

First bind an item to the shared preferences on one scene. Then close the storyboard and edit it as source (edit the xml directly).

Find the line representing the shared user preferences instance. It should look something like that:

<userDefaultsController representsSharedInstance="YES" id="a6K-Ly-rL1"/>

You can then copy this line in each scene, in the objects array, in the xml directly.

You must edit the id of each shared preference instance added manually because a storyboard file cannot have more than one object with the same id. Simply changing one character at random in the id string is enough.

Clute answered 29/8, 2015 at 22:56 Comment(2)
It's been a long time since this answer was posted and yet I also had trouble with this just now. What I did was copy the "Shared User Defaults Controller" from the one scene where it was initially to the other scene where I needed it. Then I had to go into the source for the storyboard and add representsSharedInstance="YES" to the copy and then fix a few things in the storyboard that got screwed up, such as the name of the "Bind to" object.Blus
Still need to do this in 2023, and this solution still works.Pail
K
3

My workaround is to add an NSObject to the storyboard scene and use my custom class

@objc(SharedUserDefaultsControllerProxy)
public class SharedUserDefaultsControllerProxy: NSObject {
    lazy var defaults = NSUserDefaultsController.sharedUserDefaultsController()
}

then bind on self.defaults.values.

Kiangsi answered 8/4, 2015 at 7:57 Comment(3)
Must be working but it's ugly :( Come on Apple! Get your sh*t together!Clute
I found a better workaround (in my opinion). Posted it in a separate answer. Have a look!Clute
Nice workaround. My variant is an extension on NSViewController that adds property var defaultsWorkaround: NSUserDefaultsController { return NSUserDefaultsController.sharedUserDefaultsController() }. Then I just bind to my view controller with path defaultsWorkaround.values.<whatever>.Munster

© 2022 - 2024 — McMap. All rights reserved.