Question

for the last couple of weeks I've finally gotten into Obj-C from regular C and have started my first app. I've watched tutorials and read through a book along with a lot of webpages, but I know I've only just begun. Anyway, for most of the night and this morning, I've been trying to get this code to work, and now that it will compile, I have a few warnings. I've searched and found similar problems with solutions, but still no dice. What I'm trying to do is put an array made from a txt document into the popup list in a combo box.

AwesomeBoxList.h:

    #import <Cocoa/Cocoa.h>
@interface AwesomeBoxList : NSObject 
{
  IBOutlet NSComboBox *ComboBoz;
}
-(NSArray *) getStringzFromTxtz;
- (void) awesomeBoxList;
@end

AwesomeBoxList.m:

#import "AwesomeBoxList.h"

@implementation AwesomeBoxList

-(NSArray *)getStringzFromTxtz 
{
...
return combind;
}

- (void) awesomeBoxList
{
 [ComboBoz setUsesDataSource:YES];


 [ComboBoz setDataSource:

[ComboBoz getStringzFromTxtz]: //'NSComboBox' may not respond to 'getStringzFromTxtz'

[ComboBoz comboBox:(NSComboBox *)ComboBoz objectValueForItemAtIndex: 

 [ComboBoz numberOfItemsInComboBox:(NSComboBox *)ComboBoz]]];


        /*'NSComboBox' may not respond to '-numberOfItemsInComboBox:'
   'NSComboBox' may not respond to '-comboBox:objectValueForItemAtIndex:'
   'NSComboBox' may not respond to '-setDataSource:'
  */
 }

@end

So, with all of these errors and my still shallow knowledge of Obj-C, I must be making some sort of n00b mistake.

Thanks for the help.

Was it helpful?

Solution

There does seem to be a generally large amount of confusion here, mostly manifested in your last (4) line(s) of code. You are aware that the ':' symbol is used to pass arguments to methods, not terminate a line? You are essentially daisy chaining those last 4 lines together in a way that makes no sense. As for the specific warnings, getStringzFromTxtz is a method you defined on AwesomeBoxList, not a method of NSComboBox. numberOfItemsInComboBox: and comboBox:objectValueForItemAtIndex: are NSComboBoxDataSource Protocol methods, intended to be implemented by your class on behalf of an NSComboBox, not NSComboBox methods. I would recommend doing a bit of reading on Delegates and Protocols.

To elaborate: In order for your AwesomeBoxList class to use an NSComboBox, it needs to provide information to the combo box that the combo box needs to know. In the Apple Universe, situations like this are typically handled with the Delegate Design Pattern. Specifically, the combo box needs to know how many items it will be showing as well as the object representation for each of the shown items. The combo box declares the interface for doing this in the NSComboBoxDataSource Protocol. By providing this info, you are acting as the datasource for the combo box. You can tell the combo box that it should defer to your class for its data by setting yourself as its dataSource property in Interface Builder or with a call to

[ComboBoz setDataSource:self];

from somewhere in your AwesomeBoxList's implementation. That will ensure that the combo box calls methods in your class to populate itself with info. There are two methods specifically that your AwesomeBoxList class should implement, and which will be called by the combo box:

- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index {
    switch (index) {
        // Return some object that is represented by index in the combo box
    }
}

- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox {
    // return theNumberOfItemsInYourComboBox;
}

Providing a meaningful implementation is all you need to do to populate your combo box with data. I don't know the specifics of what you want here, but that is the pattern you want to follow. Hope that helps a bit more.

OTHER TIPS

Well, the getStringzFromTxtz won't work being sent to an NSComboBox instance as that method is declared and defined in your AwesomeBoxList class. This method will not be found at runtime.

Also, I think you may need to get your head around delegates and data sources - the other methods are part of the NSComboBoxDataSource protocol. Check out the Combo Box Programming Guide in the docs for examples.

Those methods do not exist. See the NSComboBox documentation for more information.

Instead of:

[ComboBoz 
 comboBox:(NSComboBox *)ComboBoz 
 objectValueForItemAtIndex:[ComboBoz numberOfItemsInComboBox:(NSComboBox *)ComboBoz]];

Try:

[[comboBox objectValues] objectAtIndex:[comboBox numberOfItems] - 1];

Also, note that the platform convention is to use a lower-case letter at the beginning of an ivar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top