Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 878
Steven Riggs Avatar asked Mar 28 '15 00:03

Steven Riggs


2 Answers

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.

like image 180
Frizlab Avatar answered Nov 13 '22 23:11

Frizlab


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.

like image 31
phimage Avatar answered Nov 13 '22 21:11

phimage