Re: Newbie pulling my hair out regarding a simple xcode issue. Please help :-)
Re: Newbie pulling my hair out regarding a simple xcode issue. Please help :-)
- Subject: Re: Newbie pulling my hair out regarding a simple xcode issue. Please help :-)
- From: Matt Mashyna <email@hidden>
- Date: Mon, 23 Jun 2008 13:17:47 -0400
You need to think in terms of MVC - Model, View, Controller. You have
the view now you need to think of the model and controller. Make an
object in Xcode called something like MyController. For this simple
example you could combine the model and controller. Add an NSMutable
Array for your multiline.
In Xcode:
Create an IBOutlet for each of your NSTextFields (maybe firstname,
lastname, and multiline)
Create an IBAction method that you want to have called when you click
the button. Inside this function you'll do your handling of the views
-- more on that in a minute.
Save your files in Xcode and the go back to IB. In IB you need to
connect your outlets and actions. From the Library window drag a
generic object (blue cube) to your MainMenu.nib window. Using the
inspector, change the class to the one you just created in Xcode.
Control-click this new object and drag the mouse to one of the
NSTextfields. The pop up menu will let you choose from the outlets you
defined in Xcode. Hook up both fields to their respective outlets.
Then, control click from the button to your controller object's blue
cube to connect it's action.
Now you have everything hooked up and you can go back to Xcode and
finish your action method. You can refer to the text fields by the
outlet names you defined. Get the contents of line one like this
(WARNING typed without compiling):
-(IBAction)myClickAction:(id)sender
{
NSString* name = [NSString stringWithFormat:@"%@ %@", [firstname
stringValue], [lastname stringValue]]; // gets a reference to it's value
[namesArray addObject: name]; // adds it to the array
NSString* newMultiLineValue = @""; // start empty
NSEnumerator* e = [namesArray objectEnumerator];
NSString* singleName;
// loop through the names in the array to make a composite string
while(singleName = [e nextObject])
newMultiLineValue = [newMultiLineValue stringByAppendingFormat:@"%@
\n", singleName];
[line2 setStringValue: newMultiLineValue]; // sets this field
[line1 setStringValue:@""]; // clears this field
}
Ideally you want to have separate model and controller objects but
this should give you the idea of how to hook IB items to a controller.
On Jun 23, 2008, at 12:37 PM, Papa-Raboon wrote:
Hi All,
I have been trying to get my hair around Cocoa programming and have
worked through many a tutorial and it seems to be coming to me slowly.
I wondered if anyone could help me figure out a simple exercise that I
came up with myself to make it clearer how to properly address objects
with each other.
I have a simple interface in interface builder that has two text
fields (NSTextField) and a multi-line NSTextField and a button.
The two text fields are "firstName" & "lastName". The multi-line text
field is "peoplesNames" The push button is "addName".
All I need this to do is allow me to type a first name, last name,
press a button and the name gets added to the multiline text field.
Only issue is I cannot get my head around what connects to what in
Interface Builder.
I guess I need to concatenate the two name fields into a single
NSMutableString (with a space in the middle) then add them to an
NSMutableArray which is looped through to display in the multiline
text field.
This would be an insignificant task for an experienced Cocoa
programmer but would teach newbies like me a thing or two.
Many of the tutorials seem to be far too simple. And others too
complicated. Where's the intermediate ones like this.
Could anyone please help in some way either with a description of what
needs to be done or a link to a similar tutorial.
Cheers
Paul Randall
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden