带有 NSComboBox 的自定义数据源不显示任何内容

发布于 2024-11-02 22:42:02 字数 1195 浏览 8 评论 0原文

您好,我在尝试在 NSComboBox 中设置数据源时遇到以下问题。

这是我的自定义数据源类:

@interface CComboDatasource : NSObject <NSComboBoxDataSource> {
@private
    NSMutableArray* values;
}
@property (nonatomic,retain) NSMutableArray* values;
-(int)itemCount;

@end


@implementation CComboDatasource
@synthesize values;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
        values=[[NSMutableArray alloc] init];

        [values addObject:@"A"];
        [values addObject:@"B"];
        [values addObject:@"C"];
    }

    return self;
}


- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox
{
    return [values count];
}

- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index
{
    return [values objectAtIndex:index];
}

- (void)dealloc
{
    [values release];
    [super dealloc];
}

@end

稍后在另一个文件中,我将我的 IBOutlet 与我的 NSComboBox 对象 (c_box) 连接,并设置数据源 (CComboDatasource* data_source)< /代码>。

 [c_box setUsesDataSource:TRUE];
 [c_box setDataSource:data_source];
 [c_box setEditable:NO];

在执行前面的操作后,组合框中没有显示任何内容,我做错了什么?

Greetings I have the following problem trying to set a datasource in an NSComboBox.

This is my custom datasource class:

@interface CComboDatasource : NSObject <NSComboBoxDataSource> {
@private
    NSMutableArray* values;
}
@property (nonatomic,retain) NSMutableArray* values;
-(int)itemCount;

@end


@implementation CComboDatasource
@synthesize values;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
        values=[[NSMutableArray alloc] init];

        [values addObject:@"A"];
        [values addObject:@"B"];
        [values addObject:@"C"];
    }

    return self;
}


- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox
{
    return [values count];
}

- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index
{
    return [values objectAtIndex:index];
}

- (void)dealloc
{
    [values release];
    [super dealloc];
}

@end

Later in another file I connect my IBOutlet with my NSComboBox object (c_box) and I set the datasource (CComboDatasource* data_source).

 [c_box setUsesDataSource:TRUE];
 [c_box setDataSource:data_source];
 [c_box setEditable:NO];

After the previous actions nothing is displayed in the combo box, what am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉南桥 2024-11-09 22:42:02

你所拥有的对我来说基本上是正确的。我可以想到一些你可以尝试的事情:

1)尝试暂时将“return [values count]”替换为“return 5”,并将“return [values objectAtIndex:index]”替换为“return @”任意字符串“”。如果“任意字符串”出现在组合框中,您就会知道问题出在“值”数组上。

2)尝试像这样初始化“values”数组:(

values = [NSMutableArray array];

这是 NSArray 中提供的一种便捷方法。)

如果您坚持使用 alloc-init 方法,则应该以这种方式创建一个单独的临时数组,将其分配给“values”,然后释放它。否则,由于您已使用“保留”对“值”进行了属性化,因此您将保留它两次。

3) 尝试在 c_box 调用末尾添加此行:

[c_box reloadData];

并且每当您更改数据源数组时,请再次调用此行。

4)我不明白为什么将数据源类与控制组合框的类分开应该是一个问题,但如果它仍然不起作用,请尝试使拥有组合框出口的窗口/视图控制器成为实现 NSComboBoxDataSource 协议的类(numberOfItemsIn 和 objectValueFor 方法),并将“值”放入此控制器类中,或者授予此类访问“值”的权限。

希望有帮助。

What you have looks basically right to me. I can think of a few things you could try:

1) Try temporarily replacing "return [values count]" with "return 5" and replacing "return [values objectAtIndex:index]" with "return @"arbitraryString"". If "arbitraryString" then shows up in the combobox, you'll know the problem is with the "values" array.

2) Try initializing the "values" array like this:

values = [NSMutableArray array];

(It's a convenience method offered in NSArray.)

If you stick with an alloc-init method, you should make a separate temporary array that way, assign it to "values," then release it. Otherwise, since you've propertized "values" with "retain," you're retaining it twice.

3) Try adding this line at the end of your c_box calls:

[c_box reloadData];

And any time you change the data source array, call this again.

4) I don't see why separating the data source class from the class controlling the combobox should be a problem, but if it's still not working, try making the window/view controller that owns the combobox outlet the class that implements the NSComboBoxDataSource protocol (the numberOfItemsIn and objectValueFor methods), and either put "values" in this controller class or give this class access to "values."

Hope that helps.

掀纱窥君容 2024-11-09 22:42:02

好吧,我发现了问题,为了让自定义数据源类正常工作,您需要

  1. 创建一个 NSObject 并将其拖到编辑器中将
  2. 类型更改为自定义数据源类
  3. 将您的数据源声明为 IBOutlet CustomDatasourceClass* myclass
  4. 将对象与之前的插座
  5. 链接您的 NScomboBox 数据源(在 IB 设计器中)到 CustomDatasourceClass 对象

Ok I found the problem ,in order by the custom datasource class to work u need

  1. Create an NSObject and drag it to your editor
  2. Change the type to your custom datasource class
  3. Declare your Datasource as IBOutlet CustomDatasourceClass* myclass
  4. Connect the Object with the previous outlet
  5. Link your NScomboBox datasource (in IB designer) to the CustomDatasourceClass object
情感失落者 2024-11-09 22:42:02

我对组合框有同样的问题:objectValueForItemAtIndex:因为我有 10 个组合框,每个组合框我都通过以下方式检查:
if (aComboBox == _myCombo)

8 组合框工作正常,但 2 则不行。我不知道自己做错了什么,也不知道为什么别人能成功。经过大约两周的思考这个问题,我找到了解决方案。

解决方案是在从 nib 唤醒中选择选项之前重新加载数据。

[_myCombo reloadData];

I had the same problem with comboBox:objectValueForItemAtIndex: because I have 10 combo box, every combo box I checking by:
if (aComboBox == _myCombo)

8 combo box worked fine, but 2 did not. I didn't know what I was doing wrong and why others worked. After thinking about this problem about 2 weeks, I found a solution.

The solution is to reloadData before select option in awake from nib.

[_myCombo reloadData];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文