Re: Beginner question - Cocoa outlet using a method
Re: Beginner question - Cocoa outlet using a method
- Subject: Re: Beginner question - Cocoa outlet using a method
- From: Jeff Disher <email@hidden>
- Date: Sat, 25 Jan 2003 20:54:13 -0500
On Saturday, January 25, 2003, at 08:29 PM, Denis Stanton wrote:
I'm just starting with Cocoa. I've worked through some tutorial
examples and now I'm trying to branch out. I have written a very
simple extension to an example, but it doesn't work the way I expect.
Can someone tell me what I've missed?
The program displays a Table View. I'm trying to add a simple text
field to display the number of rows. I have a numberOfRowsInTableView
method so I thought it would be simple to connect this to a text field
set up in IB as follows:
-------- MyDataSource.h -----------
#import <Cocoa/Cocoa.h>
@interface MyDataSource : NSObject
{
IBOutlet NSTextField *numberOfRowsInTableView;
NSMutableArray *myArray; // initialization code not shown
}
@end
-------- MyDataSource.m --------
#import "MyDataSource.h"
@implementation MyDataSource
- (int)numberOfRowsInTableView:(NSTableView *)tableView {
return [myArray count];
}
@end
I don't really see how you are expecting your text field to get this
data. Note that this method exists for the tableview to ask its
datasource how many rows it has. Nobody else calls this method by
default.
The tableview makes you implement this so it can use it as a callback
to find out how many rows to draw space for. This is common in APIs
that implement the framwork design pattern as well as Cocoa.
This does not work. The text field connected to the
numberOfRowsInTableView outlet in Interface builder never receives the
count of the array rows.
I have found that I can get around this by declaring an instance
variable for the IBOutlet and then explicitly assigning it a value
inside the numberOfRowsInTableView method.
-------- MyDataSource.h -----------
#import <Cocoa/Cocoa.h>
@interface MyDataSource : NSObject
{
IBOutlet NSTextField *myNumberOfRows;
NSMutableArray *myArray; // initialization code not shown
}
@end
-------- MyDataSource.m --------
#import "MyDataSource.h"
@implementation MyDataSource
- (int)numberOfRowsInTableView:(NSTableView *)tableView {
[myNumberOfRows setIntValue: [myArray count]];
return [myArray count];
}
This works because you actually are setting the value in the text
field. The method you used above had no code to actually tell the
textfield what data to use and the textfield doesn't ask anyone on its
own.
Note that, although this method will work, it is not the most efficient
way of relaying this data since that particular datasource method gets
called VERY often (read: whenever the tableview tries to redraw itself
in any way). This isn't a concern for you if you are just playing
around with stuff and learning but it is probably worth noting.
This works, but it looks very clumsy to me. I would have thought that
the method that returns the rows could be connected directly to the
outlet rather than have to communicate through an extra variable.
Have I missed something basic?
Thank you for your time in reading so far
Denis
You can connect it in other ways, but the system does not know that
your textfield is interested in that information so it won't tell you
automatically.
Usually, if you wanted this information, you would write a method to
listen for a notification that data has updated white would update the
field and then you could post that notification in your datasource
whenever you actually change data. That is much more complicated,
however, so don't worry about it for now.
Hope that helps,
Jeff Disher
President and Lead Developer of Spectral Class
Spectral Class: Shedding Light on Innovation
http://www.spectralclass.com/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.