Jump to content

Setting the text of a label


2 posts in this topic

Recommended Posts

Hello Forum!

 

My first post here, and in the process of building my first application with xCode. Starting with a basic hello world type app.

 

I have created a form with interface builder which contains a button and an NStextfield. The idea is you click the button, and the textfield will change to say "Hello World".

 

I have got as far as creating the form, a controller object based on NSObject and hooking up my button click event to that object. As a test I put in an NSbeep() command in the getClick action of my controller object class. It works - so far so good.

 

What I would like to do is part 2, where I replace the NSBeep() with code to change the text of the textField I created in Interface Builder. My problem is I'm not sure how I reference the textField. I have named the object myTextField (and it appears to have an ID), but I'm lost on the referencing.

 

Can anyone help?

 

Many thanks,

 

Dean

 

PS : extremely nice forum you have here - I've spent a little time reading through the tutorials (couldn't find what I now need, but got me up and running to stage 1) and it's also a very slick and nice looking forum engine - good job :)

Link to comment
Share on other sites

Hello Forum!

 

My first post here, and in the process of building my first application with xCode. Starting with a basic hello world type app.

 

I have created a form with interface builder which contains a button and an NStextfield. The idea is you click the button, and the textfield will change to say "Hello World".

 

I have got as far as creating the form, a controller object based on NSObject and hooking up my button click event to that object. As a test I put in an NSbeep() command in the getClick action of my controller object class. It works - so far so good.

 

What I would like to do is part 2, where I replace the NSBeep() with code to change the text of the textField I created in Interface Builder. My problem is I'm not sure how I reference the textField. I have named the object myTextField (and it appears to have an ID), but I'm lost on the referencing.

 

Can anyone help?

 

Many thanks,

 

Dean

 

PS : extremely nice forum you have here - I've spent a little time reading through the tutorials (couldn't find what I now need, but got me up and running to stage 1) and it's also a very slick and nice looking forum engine - good job :D

 

Ok, lets begin. Your controller class header should be as follows:

------------------------------------------

#import <Cocoa/Cocoa.h>

 

@interface MyController : NSObject {

IBOutlet NSTextField *myLabel;

}

- (IBAction)myAction:(id)sender;

@end

------------------------------------------

And its implementation file should be:

------------------------------------------

#import "MyController.h"

 

@implementation MyController

- (IBAction)myAction:(id)sender {

// [myLabel setTitleWithMnemonic:@"Hello world"];

[myLabel setStringValue:@"Hello World"];

}

@end

------------------------------------------

You can use commented part if your label needs to have a mnemonic, but if you need just to display something you can just use setStringValue

 

This is code, now let's go to interface builder

 

1. Open your project's .nib or .xib file in interface builder

2. Drag your controller's header file from Xcode to interface builder

3. In the IB palette go to Objects & Controllers section and drag an object (plain cube) into your IB project window (window where your main menu, main window and other stuff is)

4. Now select your object and in its inspector set it's class to MyController

5. Hold Ctrl and drag your button to your controller object and connect it to myAction:

6. Hold Ctrl and drag your controller to the label in window designer and connect it to myLabel

7. Save and quit IB

8. Finally run your project in Xcode.

 

Hope this helps, but I would suggest you to go and register to developer.apple.com, where you can find everything you need to get started with Cocoa programming.

Here is the link to the excellent start up tutorial:

http://developer.apple.com/documentation/C..._section_1.html

And one more word of advice: When learning some framework or programming language NEVER do your first steps without a lot of reading, it will lead you to misunderstanding of one's concepts, waste of time and very bad habits in coding, no meter how smart you are.

Books and tutorials are written for one to learn from them, and not to try to figure how something works by himself.

 

Cheers!

Link to comment
Share on other sites

 Share

×
×
  • Create New...