I’ve registered the transformer in the AppDelegate:
[code] dynamic var isNotEmptyTransformer = IsNotEmptyTransformer()
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
NSValueTransformer.setValueTransformer(isNotEmptyTransformer, forName: "IsNotEmptyTransformer")
addWindowController()
}
[/code]
And, I’ve set the Send button’s Value binding to message in the Model Key Path and in the ValueTransformer I’ve set the IsNotEmptyTransformer class. I also set the message text field’s Continuously Updates Value on.
When I run the program, the Send button is always disabled. Entering text into the message text field does not change the button’s enabled state. I’ve also set breakpoints on all the IsNotEmptyTransformer’s methods and none of them is ever being called. I’ve verified in the debugger that the IsNotEmptyTransformer has in fact been registered. I’m at a dead end, I don’t understand why the transformer’s methods are not being called. What am I missing that is preventing the button from becoming enabled with the text entry?
Hi, after much effort I finally figured it out.
transformedValueClass: must return an NSNumber
class func transformedValueClass(value: AnyObject?) -> AnyObject? {
return NSNumber.self
}
transformedValue: is not a class method
override func transformedValue(value: AnyObject?) -> AnyObject? {
if value == nil {
println("Message is nil")
return NSNumber(bool: false)
}
let message = value as! String
let answer = !message.isEmpty
return NSNumber(bool: answer)
}
and you register the transformer in AppDelegate’s applicationDidFinishLaunching: method
Under the Send button’s Availability section set Model Key Path to message and Value Transformer to isNotEmptyValueTransformer. Note that it begins with a lowercase i, meaning that instead of the class name you should provide the name under which you registered it in the AppDelegate.
with
ValueTransformer.setValueTransformer(IsNotEmptyTransformer(), forName: NSValueTransformerName(“IsNotEmptyTransformer”))
in applicationDidFinishLaunching of AppDelegate. Finally, I also had to make an outlet to the sendButton just to be able to disable it once the text had been sent.