JordanD
JordanD

Reputation: 163

Calling getter methods from another class

When I try to call the getter methods "gender" and "teamName" from another class (by creating an instance of the TeamsViewController class and calling the methods on that instance), the methods return null. Why is this?

Thanks for your help.

@interface TeamsViewController () {
    NSString *teamName;
    NSString *gender;
}
@end

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    gender = @"boys";
    teamName = @"Basketball";
}

-(NSString *)gender {
    NSLog(@"returning %@",gender);
    return gender;
}
-(NSString *)teamName {
    NSLog(@"returning %@",teamName);
    return teamName;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (onBoys==true) {
        gender = @"boys";
    }
    else {
       gender = @"girls";
    }

    [self performSegueWithIdentifier:@"teamsPushSegue" sender:self];
}



TeamsViewController *teamsInstance = [[TeamsViewController alloc]init];     
[teamsInstance gender];

Upvotes: 1

Views: 378

Answers (2)

Rob
Rob

Reputation: 437392

By the way, you might want to avail yourself of public properties, rather than explicitly defining instance variables and getter methods yourself. For example:

TeamsViewController.h:

@interface TeamsViewController : UIViewController

@property (nonatomic, copy) NSString *teamName;
@property (nonatomic, copy) NSString *gender;

@end

TeamsViewController.m

// @interface TeamsViewController () {
//     NSString *teamName;
//    NSString *gender;
// }
// @end

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.gender = @"boys";
    self.teamName = @"Basketball";
}

// if you use properties, you don't have to write these methods; they'll be synthesized for you
//
// -(NSString *)gender {
//      NSLog(@"returning %@",gender);
//      return gender;
// }
//
// -(NSString *)teamName {
//     NSLog(@"returning %@",teamName);
//     return teamName;
// }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (onBoys==true) {
        self.gender = @"boys";
    }
    else {
        self.gender = @"girls";
    }

    [self performSegueWithIdentifier:@"teamsPushSegue" sender:self];
}

Or if these are private properties, you could put the property declarations in the private class extension (but then I don't know why you bothered to write getter methods in the first place, if they were private).

For more information, see the Encapsulating Data section of the Programming with Objective-C guide.

Upvotes: 4

trojanfoe
trojanfoe

Reputation: 122381

Creating an instance of the class with:

TeamsViewController *teamsInstance = [[TeamsViewController alloc] init];

Won't call the method viewDidLoad where those instance variables are initialised.

Instead initialise them in an init method:

- (id)init {
    self = [super init];
    if (self) {
        gender = @"boys"; 
        teamName = @"Basketball";
    }
    return self;
}

However the method you probably want to override is initWithNibName:bundle: and you probably want to avoid calling the init method at all.

Upvotes: 5

Related Questions