instance variable isn't working

Discussion in 'Mac Programming' started by Pyr0, Mar 28, 2009.

  1. macrumors newbie

    Joined:
    Mar 28, 2009
    Messages:
    4
    #1
    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
    
    
     
  2. macrumors 6502a

    Joined:
    Oct 29, 2006
    Messages:
    807
    Location:
    Virginia
    #2
    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.
     
  3. macrumors newbie

    Joined:
    Mar 28, 2009
    Messages:
    4
    #3
    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.
     
  4. macrumors 6502a

    Joined:
    Oct 29, 2006
    Messages:
    807
    Location:
    Virginia
    #4
    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.
     
  5. macrumors newbie

    Joined:
    Mar 28, 2009
    Messages:
    4
    #5
    Ok so I have to use "self" insted of making a new instance right?
     
  6. macrumors 6502a

    Joined:
    Oct 29, 2006
    Messages:
    807
    Location:
    Virginia
    #6
    Yep, that should do it.
     
  7. macrumors newbie

    Joined:
    Mar 28, 2009
    Messages:
    4
    #7
    Awesome:D thanks for the help, I don't think I would've got very far as a programmer without it.
     
  8. macrumors 6502a

    Joined:
    Oct 29, 2006
    Messages:
    807
    Location:
    Virginia
    #8
    Oh, no problem. So I take it you got it working? Congrats!
     

Share This Page