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: Denis Stanton <email@hidden>
- Date: Sun, 26 Jan 2003 15:51:21 +1300
Hi Jeff thanks for the quick response.
On Sunday, January 26, 2003, at 02:54 PM, Jeff Disher wrote:
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.
I was expecting that as the window was build up from the nib it would
find that the value of this field was connected to the
numberOfRowsInTableView method and would call the method to get the
value. Maybe I'm too stuck in the WebObjects paradigm where each
layout object in turn follows its link to the Java code to get its
value from either a variable or a method.
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.
I understand that the numberOfRowsInTableView is required for
displaying a table view. I hoped to use the same method to return the
number of rows so that I can display it. The framework must be calling
this method at least once and I am able to use it to set an int
variable which is defined to be an outlet to my window. I had hoped
that the numberOfRowsInTableView method itself could be connected to an
outlet so every time the field had to be displayed it would call the
method. I realise that I have misunderstood something here.
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.
I hoped that "IBOutlet NSTextField *myNumberOfRows;" would tell the
textfield to get its value by calling
"(int)numberOfRowsInTableView:(NSTableView *)tableView {....."
When this didn't work I deliberately got the code to set the value into
the field where the outlet picks it up. I was assuming that the outlet
connection caused the value to be requested, either from an instance
variable (which appears to work) or from an method (which doesn't).
I'm slowly coming to understand that in Cocoa-IB the interface doesn't
request the value, its more the case that the outlet send it's value
when it changes. Is that right? The instance variable puts a value on
the window whenever its value changes. In that case why doesn't each
call of the method cause the returned value to be echoed on the screen
when there is line connecting the method name to the outlet?
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).
That's the sort of thing I feared when I said my work around was clumsy.
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.
OK, this is where I have got it wrong. I thought the outlet connection
was there to tell the screen object (my text field) where to get its
value (WebObjects-thinking here)
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.
So you're saying I will will always have to set an instance variable to
the value I want displayed, rather than expect the outlet to use the
method. The fault in my code is that I am setting this variable in a
very inefficient place.
Thanks for your help
Denis
_______________________________________________
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.