PDA

View Full Version : instance variable isn't working




Pyr0
Mar 28, 2009, 07:36 PM
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:

#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
Mar 28, 2009, 09:03 PM
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:


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


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

Pyr0
Mar 29, 2009, 05:06 PM
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
Mar 29, 2009, 05:15 PM
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
Mar 29, 2009, 05:41 PM
Ok so I have to use "self" insted of making a new instance right?

eddietr
Mar 29, 2009, 06:26 PM
Ok so I have to use "self" insted of making a new instance right?

Yep, that should do it.

Pyr0
Mar 29, 2009, 11:53 PM
Awesome:D thanks for the help, I don't think I would've got very far as a programmer without it.

eddietr
Mar 30, 2009, 09:36 AM
Awesome:D thanks for the help, I don't think I would've got very far as a programmer without it.

Oh, no problem. So I take it you got it working? Congrats!