RE: cocoa-dev digest, Vol 2 #1610 - 14 msgs
RE: cocoa-dev digest, Vol 2 #1610 - 14 msgs
- Subject: RE: cocoa-dev digest, Vol 2 #1610 - 14 msgs
- From: Tom Davie <email@hidden>
- Date: Tue, 3 Dec 2002 16:35:55 -0000
>
Message: 13
>
Date: Tue, 3 Dec 2002 07:49:09 -0800
>
Subject: Getting started in Cocoa and IB
>
From: John Clark <email@hidden>
>
To: email@hidden
>
>
Recently I've decided to overcome my deficiencies in user interface
>
programming. Usually I've mucked around at the kernel level
>
and command
>
line tools are enough.
>
>
However, I need to design user interfaces and would like to use
>
InterfaceBuilder
>
and the Cocoa object environment.
>
>
However, my very simplistic first try seems to be very
>
frustrating. What
>
I need is
>
a hint on how to have a NSTextField result in an action which
>
takes the
>
string
>
typed by the user, and placed in a regular 'char' variable
>
for use in a
>
set of
>
standard C functions.
>
>
While Cocoa book has a very simple intro project involving a
>
few buttons
>
and
>
an output text field, which I was able to follow and get working, the
>
complementary
>
operation of inputing text seemed difficult to find.
>
>
Could someone point to an example of simple text input to a field and
>
have
>
the action pick up the data.
Well, I'm assuming that you submit this button by clicking on some button or
other, in which case you will have an action associated with that button.
First, you need to connect to that textFeild using an outlet. You now have
some way of getting at it, so now you need to get the text out. Simple...
look up NSTextFeild in the app kit docs... No obvious methods, but it
inherits from NSControl, so let's look at it's documentation. NSControl has
the method stringValue
soo.... [myTextFeild stringValue]
But wait, that returns an NSString, and you want to be able to put this into
a standard C application... time to go look up the NSString docs... woohoo
- (void)getCString:(char *)buffer
sooo.... [[myTextFeild stringValue]
getCString:myCStringThatIsHopefullyLongEnough]
Well... might as well make it a bit more reliable... so... look up the
NSString docs a bit more and we find cStringLength
- (IBAction)buttonClicked:(id sender)sender
{
NSString feildContents;
char myStringContents [MAX_STRING_LENGTH];
feildContents = [myTextFeild stringValue]; // Note I didn't retain it
so I shouldn't release it
if ([feildContents cStringLength] >= MAX_STRING_LENGTH)
[feildContents getCString:myStringContents];
else
// Throw exception or trim the string or something
// Do your funky stuff.
}
_______________________________________________
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.