CoreData 整数属性上的 NSPopupButton 的 NSValueTransformer
我有一个 CoreData/SQLite 应用程序,其中有一个 NSPopupButton,显示某些实体属性(整数)的可能值。它会自动显示数据库中的属性值。用户必须能够分配另一个值,以便我的代码在启动时生成所有可能值的数组。在我的 XIB 中,我有一个用于按钮的 NSObject 和一个用于可能值的 NSArrayController。绑定已完成,一切正常。
当然,仅使用数字对于用户来说并不清楚,因此我创建了一个值转换器来用弹出按钮上的单词替换数字:1=>Red,2=>Green,3=>Blue。
问题是只有选定的 Popup 项目被单词替换。当我使用弹出按钮选择另一个值来修改实体属性时,它不会保存它。我通过在 TableView 中选择另一个对象然后返回到修改后的对象来看到它,弹出窗口将指示 NoValue。
我不明白为什么。感谢您的帮助。
以下是头文件“ColorOption.h”的代码:
#import <Foundation/Foundation.h>
@interface ColorOption : NSArrayController {
IBOutlet NSPopUpButton *colorPopup;
NSArray *theNumbers;
}
@property (retain) NSPopUpButton *colorPopup;
@property (readwrite, copy) NSArray *theNumbers;
@end
@interface StatusTransformer : NSValueTransformer {
}
@end
以下是实现文件“ColorOption.m”的代码:
#import "ColorOption.h"
@implementation ColorOption
@synthesize colorPopup;
@synthesize theNumbers;
-(void)awakeFromNib {
[self setTheNumbers:[NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],[NSNumber numberWithInt:3],nil]];
StatusTransformer *statusTrans = [[[StatusTransformer alloc] init] autorelease];
[NSValueTransformer setValueTransformer:statusTrans forName:@"StatusTransformer"];
}
@end
@implementation StatusTransformer
+ (Class)transformedValueClass { return [NSString class]; }
+ (BOOL)allowsReverseTransformation { return YES; }
- (id)transformedValue:(id)value {
switch ([value intValue]) {
case 1:
return @"Red";
break;
case 2:
return @"Green";
break;
case 3:
return @"Blue";
break;
default:
return @"";
break;
}
}
-(id)reverseTransformedValue:(id)value {
if ([@"Red" isEqualToString:value]) return [NSNumber numberWithInt:1];
if ([@"Green" isEqualToString:value]) return [NSNumber numberWithInt:2];
if ([@"Blue" isEqualToString:value]) return [NSNumber numberWithInt:3];
return nil;
}
@end
I have a CoreData/SQLite app with an NSPopupButton presenting the possible values for some entity attribute (an integer). It automatically shows the attribute value in the database. The user must be able to assign another value so my code generates an array of all possible values on start-up. In my XIB, I have a NSObject for the button and an NSArrayController for the possible values. Bindings are done and everything works fine.
Of course, using only numbers is not clear for the user, so I created a value transformer to replace numbers by words on the popup button: 1=>Red, 2=>Green, 3=>Blue.
The problem is that only the selected Popup item is replaced by a word. And when I choose another value with the Popup button to modify the entity attribute, it doesn't save it. I see it by selecting another object in the TableView then coming back to the modified one and the Popup will indicate NoValue.
I don't understand why. Thank you for your help.
Here is the code of the header file "ColorOption.h":
#import <Foundation/Foundation.h>
@interface ColorOption : NSArrayController {
IBOutlet NSPopUpButton *colorPopup;
NSArray *theNumbers;
}
@property (retain) NSPopUpButton *colorPopup;
@property (readwrite, copy) NSArray *theNumbers;
@end
@interface StatusTransformer : NSValueTransformer {
}
@end
Here is the code of the implementation file "ColorOption.m":
#import "ColorOption.h"
@implementation ColorOption
@synthesize colorPopup;
@synthesize theNumbers;
-(void)awakeFromNib {
[self setTheNumbers:[NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],[NSNumber numberWithInt:3],nil]];
StatusTransformer *statusTrans = [[[StatusTransformer alloc] init] autorelease];
[NSValueTransformer setValueTransformer:statusTrans forName:@"StatusTransformer"];
}
@end
@implementation StatusTransformer
+ (Class)transformedValueClass { return [NSString class]; }
+ (BOOL)allowsReverseTransformation { return YES; }
- (id)transformedValue:(id)value {
switch ([value intValue]) {
case 1:
return @"Red";
break;
case 2:
return @"Green";
break;
case 3:
return @"Blue";
break;
default:
return @"";
break;
}
}
-(id)reverseTransformedValue:(id)value {
if ([@"Red" isEqualToString:value]) return [NSNumber numberWithInt:1];
if ([@"Green" isEqualToString:value]) return [NSNumber numberWithInt:2];
if ([@"Blue" isEqualToString:value]) return [NSNumber numberWithInt:3];
return nil;
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我正在审查我的问题,发现这个问题尚未得到解答。然而与此同时,我已经找到了答案,我现在将发布它,这样它可能会帮助其他人。
我犯的错误是定义了整数值数组,而我应该做的是定义填充弹出按钮的单词数组。下面是完整的解决方案:
头文件:
实现文件
现在,在 Xcode 的绑定检查器中,值转换器必须应用于弹出按钮的“内容值”和“选定对象”。
I'm reviewing my questions and see that this one has been left unanswered. However in the meantime I had found the answer which I'll post now so it may help others.
The mistake I did was to define the array of integer values while what I should have done is to define the array of words populating the popup button. Here is the full solution:
Header file :
Implementation file
Now in the bindings inspector of Xcode, the value transformer must be applied to "Content Values" and "Selected Object" for the popup button.