用外部类数组中生成的数据填充 NSComboBox
在我的最后一个问题之后,关于访问不同类的数组,我遇到了一个新问题,这让我头痛了三天。每当我认为我有正确的解决方法时,我都会失败。
嗯...我对 Cocoa 编程还没有太多经验。但也许你能给我缺失的提示。
让我向您展示我选择的方法:
1) 在类 PortConnection.h/.m 中声明数组
@interface PortConnection : NSObject {
@private
NSMutableArray *baudArray;
}
@property (nonatomic, retain) NSMutableArray *baudArray;
并在 .m 中合成
@implementation PortConnection
@synthesize baudArray;
接下来我决定在 ViewController 中实现一个负责填充的方法包含我需要显示的数据的数组。该类的名称是“PortTableViewController.h”
#import "PortConnection.h"
@interface PortTableViewController : NSObject <NSTableViewDataSource, NSComboBoxDataSource> {
@private
IBOutlet NSComboBox *baudSelection;
PortConnection *portConnection;
}
@property (assign) IBOutlet NSTableView *portTableView;
- (IBAction)fillBaudSelection:(id)sender;
@end
,我的方法“fillBaudSelection”的实现。
- (IBAction)fillBaudSelection:(id)sender {
int baudCount = [portConnection.baudArray count];
int i;
for (i = 0; i <= baudCount; i++){
[baudSelection addItemWithObjectValue:[portConnection.baudArray objectAtIndex:i]];
}
}
此外,我还实现了组合框的委托方法。
- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index{
return [portConnection.baudArray objectAtIndex:index];
}
- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox{
return [portConnection.baudArray count];
}
我的问题是:
1)我是否需要对组合框使用委托方法? 2)尽管数组中充满了数据,但组合框根本没有填充数据 3)我想得太复杂了吗?
非常感谢你给我的每一个提示!
此致 塞巴斯蒂安
after my last question, regarding accessing an array from a different class, I ran into an new problem, that's giving me a headache for three days now. Everytime I think I have the correct solution approach, I fail.
Well... I don't have many experience yet regarding Cocoa Programming. But maybe you are able to give me the missing hint.
Let me show you what approach I've chosen:
1) the declaration of an array in the class PortConnection.h/.m
@interface PortConnection : NSObject {
@private
NSMutableArray *baudArray;
}
@property (nonatomic, retain) NSMutableArray *baudArray;
and the synthesize in .m
@implementation PortConnection
@synthesize baudArray;
Next I decided to implement a method in the ViewController that should be in charge of filling the array with data I need for display. The name of the class is "PortTableViewController.h"
#import "PortConnection.h"
@interface PortTableViewController : NSObject <NSTableViewDataSource, NSComboBoxDataSource> {
@private
IBOutlet NSComboBox *baudSelection;
PortConnection *portConnection;
}
@property (assign) IBOutlet NSTableView *portTableView;
- (IBAction)fillBaudSelection:(id)sender;
@end
and the implementation of my method "fillBaudSelection".
- (IBAction)fillBaudSelection:(id)sender {
int baudCount = [portConnection.baudArray count];
int i;
for (i = 0; i <= baudCount; i++){
[baudSelection addItemWithObjectValue:[portConnection.baudArray objectAtIndex:i]];
}
}
Furthermore I implemented the delegate methods for the combobox.
- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index{
return [portConnection.baudArray objectAtIndex:index];
}
- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox{
return [portConnection.baudArray count];
}
My questions are:
1) Do I need to use the Delegate Methods for a combo box at all?
2) the Combobox isn't filled with data at all, though the array is filled with data
3) Am I thinking to complicated??
Thanks so much for every hint I get from you!
best Regards
Sebastian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定您正确连接了组合框吗?确保委托和数据源设置为实现了方法的任何类。
Are you sure you hooked the combobox correctly? make sure the delegate and the datasource are set to whatever class has the methods implemented.