Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / August 2005



Tip: Looking for answers? Try searching our database.

Obtaining textfield within a window

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Keith Wiley - 13 Aug 2005 09:46 GMT
Assuming I have a pointer to an NSPanel, say, can I obtain pointers to its
NSTextField objects?  I realize that one way to do this is make a class in
IB, create outlets for each textfield I'm interested in, and
control-click-drag to connect the class to the textfields.

But can I do that from code at runtime instead?  There area bunch of
textfields and it seems cluttering to have gobs of member variables
pointing at each individual textfield, plus, I don't need the textfield
pointers as permament member variables.  I just need them briefly from
time to time.

Can I directly ask for them somehow?  The NSPanel and NSWindow classes
don't have any obvious looking functions (messages) for obtaining control
objects contained within themselves.  I'm a little surprised.

Thanks.

________________________________________________________________________
Keith Wiley         kwiley@cs.unm.edu         http://www.unm.edu/~keithw

"Yet mark his perfect self-contentment, and hence learn his lesson,
that to be self-contented is to be vile and ignorant, and that to
aspire is better than to be blindly and impotently happy."
                                           --  Edwin A. Abbott, Flatland
________________________________________________________________________
Ulrich Hobelmann - 13 Aug 2005 11:34 GMT
> But can I do that from code at runtime instead?  There area bunch of
> textfields and it seems cluttering to have gobs of member variables
> pointing at each individual textfield, plus, I don't need the textfield
> pointers as permament member variables.  I just need them briefly from
> time to time.

You could subclass the panel and give it one or more methods that
returns those textfields.

> Can I directly ask for them somehow?  The NSPanel and NSWindow classes
> don't have any obvious looking functions (messages) for obtaining
> control objects contained within themselves.  I'm a little surprised.

No, but I'm sure at least your panel knows its fields and probably has
them stored as variables.  So by giving it a method other objects can
ask the panel for that information.

Signature

I believe in Karma.  That means I can do bad things to people
all day long and I assume they deserve it.
    Dogbert

Patrick Machielse - 13 Aug 2005 13:06 GMT
> There area bunch of textfields and it seems cluttering to have gobs of
> member variables pointing at each individual textfield, plus, I don't need
[quoted text clipped - 4 lines]
> don't have any obvious looking functions (messages) for obtaining control
> objects contained within themselves.  I'm a little surprised.

use:

   NSTextField *field = [[panel contentView] viewWithTag:tag];

where panel is some NSWindow object pointer and tag is simply an int you
can set in the IB inspector.

patrick
Keith Wiley - 13 Aug 2005 18:31 GMT
> use:
>
>    NSTextField *field = [[panel contentView] viewWithTag:tag];
>
> where panel is some NSWindow object pointer and tag is simply an int you
> can set in the IB inspector.

Works like a charm.  I'm didn't realize that panel function I needed was
contentView.  Thanks.

________________________________________________________________________
Keith Wiley         kwiley@cs.unm.edu         http://www.unm.edu/~keithw

"Yet mark his perfect self-contentment, and hence learn his lesson,
that to be self-contented is to be vile and ignorant, and that to
aspire is better than to be blindly and impotently happy."
                                           --  Edwin A. Abbott, Flatland
________________________________________________________________________
Chris Hanson - 14 Aug 2005 01:04 GMT
> Assuming I have a pointer to an NSPanel, say, can I obtain pointers to
> its NSTextField objects?  I realize that one way to do this is make a
> class in IB, create outlets for each textfield I'm interested in, and
> control-click-drag to connect the class to the textfields.

This is the usual way to do such things.  You have a controller object
that has outlets to the views that it cares about.

Starting with Panther, you could also just use one of the reusable
controllers and bind the value of that text field through a controller
to a model object.  In this case, you wouldn't need an outlet to the
text field, because you wouldn't have code that cares about the text
field itself at all.

> But can I do that from code at runtime instead?  There area bunch of
> textfields and it seems cluttering to have gobs of member variables
> pointing at each individual textfield, plus, I don't need the textfield
> pointers as permament member variables.  I just need them briefly from
> time to time.

You *can* do this -- it's just a matter of walking the view hierarchy
-- but this is not the typical way things are done in Cocoa.  Either
use outlets to refer to the text fields explicitly, or use bindings on
the text fields so you never need to manipulate them directly at all.

> Can I directly ask for them somehow?  The NSPanel and NSWindow classes
> don't have any obvious looking functions (messages) for obtaining
> control objects contained within themselves.  I'm a little surprised.

Controls are just a special kind of view.  You can get your window's
content view, and then walk the view hierarchy.  But because Cocoa is
designed to use outlets in this situation, you'll have to figure out
your own way of identifying which controls you care about.  You could
use tags for this if you really wanted to.  But I would *strongly*
encourage you to use either outlets or bindings instead, since that's
how the framework is intended to be used.

 -- Chris
Keith Wiley - 14 Aug 2005 02:43 GMT
> This is the usual way to do such things.  You have a controller object that
> has outlets to the views that it cares about.

and if a panel has numerous textfields, then it would be appropriate for
the subclass I create to have a large number of outlets?  It seems like a
"crowded" approach, but if that's the "right" way, I want to learn it
right.  I'm just not sure if that is the right way.  For example, I'm
making a high scores panel for a game.  I have twenty text fields, ten
names, and ten scores.  Do I actually create twenty outlets in IB, and
control-click-drag twenty times to connect the outlets?  Or is this a
situation where accessing the textfields through the contextView (using
tags as unique identifers) is an acceptable approach?

I already have it working using the contextView and tags, but if making a
bunch of outlets is the "cocoa" way to do it, I would rather go back and
do it right.

> Starting with Panther, you could also just use one of the reusable
> controllers and bind the value of that text field through a controller to a
> model object.  In this case, you wouldn't need an outlet to the text field,
> because you wouldn't have code that cares about the text field itself at all.

Hmmm, does that give me two way access to the textfields?  I need to write
to them to write the player names and high scores, but I also need to read
from one of them when the panel is done since the user types in his name
if he gets a high score.  So I need both read and write access to the
textfields.

I looked up bindings on dev.apple.com and didn't get many hits btw.

> you really wanted to.  But I would *strongly* encourage you to use either
> outlets or bindings instead, since that's how the framework is intended to be
> used.

Cool.  Thanks.

________________________________________________________________________
Keith Wiley         kwiley@cs.unm.edu         http://www.unm.edu/~keithw

"Yet mark his perfect self-contentment, and hence learn his lesson,
that to be self-contented is to be vile and ignorant, and that to
aspire is better than to be blindly and impotently happy."
                                           --  Edwin A. Abbott, Flatland
________________________________________________________________________
Chris Hanson - 14 Aug 2005 07:21 GMT
> and if a panel has numerous textfields, then it would be appropriate
> for the subclass I create to have a large number of outlets?  It seems
> like a "crowded" approach, but if that's the "right" way, I want to
> learn it right.

It is the right way, but I want to be clear about something: The
controller I'm suggesting shouldn't be a subclass of NSPanel.  
Generally you would only subclass NSPanel, NSWindow, etc. if you're
creating a new-kind-of-panel or new-kind-of-window (e.g. a round
window) rather than just putting-stuff-in.

> I'm just not sure if that is the right way.  For example, I'm making a
> high scores panel for a game.  I have twenty text fields, ten names,
> and ten scores.  Do I actually create twenty outlets in IB, and
> control-click-drag twenty times to connect the outlets?  Or is this a
> situation where accessing the textfields through the contextView (using
> tags as unique identifers) is an acceptable approach?

For something like that, it might be more worthwhile to use an NSForm
or NSMatrix.  These let you index into a collection of cells (either by
index, or by row/column), which may fit what you're trying to do more
closely.

> Hmmm, does that give me two way access to the textfields?  I need to
> write to them to write the player names and high scores, but I also
> need to read from one of them when the panel is done since the user
> types in his name if he gets a high score.  So I need both read and
> write access to the textfields.

When you change the value of a bound property in a model object,
anything bound to that property will be updated to reflect the new
value.

When you manipulate a view that is bound to a property of a model
object, the property will be changed accordingly.  (And anything else
bound to that property will be updated to reflect the new value.)

> I looked up bindings on dev.apple.com and didn't get many hits btw.

I just searched developer.apple.com for "cocoa bindings" (no quotes)
using the search field on the main page.  I got back 459 hits, the
first five of which are:

 Cocoa Bindings Programming Topic: What Are Cocoa Bindings?
 Developing Cocoa Applications Using Bindings: A Tutorial
 Cocoa Bindings Programming Topic: Troubleshooting Cocoa Bindings
 Cocoa Bindings Programming Topics: Introduction to Cocoa Bindings
 Cocoa Performance Guidelines: Cocoa Bindings Tips

There should be a lot of information on bindings available from both
developer.apple.com and in the ADC Reference Library accessible via
Xcode.

I'll grant that the first few hits if you search for just "bindings"
are WebObjects-related.

 -- Chris
Patrick Machielse - 14 Aug 2005 10:54 GMT
> > But I would *strongly* encourage you to use either outlets or bindings
> > instead, since that's how the framework is intended to be used.
>
> Cool.  Thanks.

All methods of getting a reference to the text field are equally valid.
They are _all_ in the framework, and they are in there to be used. Use
the method that suits your needs best. Depending on the situation that
may be enumerating an NSMatrix, using Bindings, or indeed -viewWithTag:

patrick
Keith Wiley - 14 Aug 2005 17:02 GMT
> All methods of getting a reference to the text field are equally valid.
> They are _all_ in the framework, and they are in there to be used. Use
> the method that suits your needs best. Depending on the situation that
> may be enumerating an NSMatrix, using Bindings, or indeed -viewWithTag:

:-)  Appreciate the support.

Cheers!

________________________________________________________________________
Keith Wiley         kwiley@cs.unm.edu         http://www.unm.edu/~keithw

"Yet mark his perfect self-contentment, and hence learn his lesson,
that to be self-contented is to be vile and ignorant, and that to
aspire is better than to be blindly and impotently happy."
                                           --  Edwin A. Abbott, Flatland
________________________________________________________________________
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.