views:

385

answers:

1

I'm a complete newbie to Objective-C, coming from a background of PHP, assorted dialects of BASIC, and a smattering of C/C++. While I feel I've got a fair handle on setting basic windows up in Interface Builder, however, I'm having trouble manipulating the content of text fields, especially calling values from them and sending values to them.

I'm using a basic Xcode Cocoa application template with no extras, and created a Cocoa class called "controller.h". In Interface Builder I put together a window with a Secure Text field, a basic NSTextField under it, and a Push Button to submit the form. My controller class is like so:

//controller.h
#import <Cocoa/Cocoa.h>

@interface controller {
    IBOutlet id userInput;
    IBOutlet id userOutput;
}
- (IBAction)submitButton:(id)sender;
@end

//controller.m
#import "controller.h"

@implementation controller
- (IBAction)submitButton:(id)sender {
    NSBeep();
    [userOutput setStringValue: userInput];
    [userInput setStringValue:nil];
}
@end

I have userInput linked up to the Secure Text field, and userOutput linked to the NSTextField. submitButton is linked to the Push Button. Based on my understanding of how things work, this should assign the value of the secure text in userInput to userOutput, thereby updating what is displayed in the NSTextField, and then reset the field. The beep is there to let me know if the function is being called.

The beep isn't going off, however, which means that nothing is happening. Interface Builder built the files though, so I know that they've been defined properly and are connected to the proper places. How do I fix it so that the IBAction executes and the values are sent where they need to be?

+1  A: 

A more effective way to see if it's being called is to set a breakpoint (double click in the gutter to the left of the editor) and debug the app instead. The first suggestion is always to sanity-check your connections.

In addition, you'll probably want to use this line instead:

[userOutput setStringValue:[userInput stringValue]];

(Note that -stringValue is inherited from NSControl, just like -setStringValue: is.)


EDIT: It might help to make sure you're actually instantiating the controller object. You can add an -awakeFromNib method to the controller and put a breakpoint in it to see whether this is the case. (See http://www.cocoadev.com/index.pl?AwakeFromNib) If that method isn't getting called, then the connections specified in IB won't be made, either.

Quinn Taylor
Thanks for helping with the text field question and pointing out how to use breaks! Any insight as to why the IBAction isn't activating, by any chance?
Kaji
No problem. I've updated my answer with another idea. IB-related issues are quite hard to debug without actually being in front of the screen, but I hope this helps.
Quinn Taylor
All right, went through and tinkered with how I defined and connected the outlets and actions in the class definition, and everything seems to be working now! awakeFromNib activated, and so I removed that line and set the button function to [userOutput setStringValue:[userInput stringValue]];and it's doing exactly what it's supposed to do now. Thanks a lot!
Kaji