Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Pyr0

macrumors newbie
Original poster
Mar 28, 2009
4
0
Um I'm really new at programming and I just had a question about why something isn't working. I trying to get a NSTextField object named label to display what ever text i give it, but it wont display anything. What the crap am I doing wrong?:mad:

Code:
#import <Cocoa/Cocoa.h>


@interface Count : NSObject {
	IBOutlet NSTextField *textField;
	IBOutlet NSTextField *label;
	NSString *textFieldString;
}

-(IBAction)count: (id)sender;
-(void)getText;
-(void)setLabel;
@end

#import "Count.h"


@implementation Count

-(IBAction)count: (id)sender
{
	Count *appController = [[Count alloc] init];
	
	[appController getText];
	
	[appController setLabel];
}

-(void)getText
{
	textFieldString = [textField stringValue];
}

-(void)setLabel
{
	[label setStringValue:@"hi"];
}
@end
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
I assume count: is wired to a button in your interface?

So one problem is that count: is creating a new instance of class Count, and then calling other methods on this new instance. Which probably means that "label" and "textField" in that new instance are nil.

Assuming everything is wired up correctly in IB, I would think deleting this line:

Code:
Count *appController = [[Count alloc] init];

would be closer to what you want. I'm making assumptions about what you actually want, of course.
 

Pyr0

macrumors newbie
Original poster
Mar 28, 2009
4
0
First off sorry for not Being clear. You assumed correctly, that's exactly what I'm doing. Secondly if I delete that line how do I call my methods? I thought you had to use an object to call methods.
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
Well, you already have an instance (an object) of type Count.

It is that instance that is receiving this count: method. So you have an object getting a message "count:" but then in handling that message it is creating another instance. And then telling that other instance to getText and setLabel.

I'll bet the problem is that the other instance has nothing in its textField and label outlets.

If you had an instance of Count in your Nib, and you connect is outlets (textField and label) to a UITextField and a UILabel, then it's important to understand that you have created an object of type Count in that Nib. And that object (and that object alone) has its outlets set correctly. You aren't setting that outlet for all Counts, you are setting those outlets for that particular instance of Count in that particular nib.

If your program goes and create another instance of Count, then you're going to have to set that instance's outlets also. But I think that's probably not what you are trying to do here.
 

Pyr0

macrumors newbie
Original poster
Mar 28, 2009
4
0
Ok so I have to use "self" insted of making a new instance right?
 

Pyr0

macrumors newbie
Original poster
Mar 28, 2009
4
0
Awesome:D thanks for the help, I don't think I would've got very far as a programmer without it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.