views:

31

answers:

2

I'm building an iphone app and I've got a table view with some textfields inside the cells, the content of the fields is set in viewWillAppear (its a grouped TableView w/ 3 fields that are always the same). The content of the text fields is retrieved from getter methods that return values from various class variables.

The problem I'm having is the getter seems to be returning the original value, not the value that is modified by the setter method. The class variable is an NSMutableString. Is it possible the view is caching the method call?

//header file
@implementation ManageWorkoutViewController : UIViewController {
    NSMutableString *workoutDifficulty;
}

-(void)setWorkoutDifficulty:(NSString *)value;
-(NSString *)getWorkoutDifficulty;

@end



//implementation file
-(NSString *)getWorkoutDifficulty {

    if (nil == workoutDifficulty) {
        workoutDifficulty = [NSMutableString stringWithString:@"Easy"];
    }

    NSLog(@"getter: Returning workoutDifficulty as: %@", workoutDifficulty);

    return workoutDifficulty;

} //end getWorkoutDifficulty



-(void)setWorkoutDifficulty:(NSString *)value {

    workoutDifficulty = [NSString stringWithFormat:@"%d", value];
    NSLog(@"setter: workoutDifficulty set as: %@", workoutDifficulty);

}//end setWorkoutDifficulty


//elsewhere in the implementation another table view is 
//pushed onto the nav controller to allow the user to pick
//the difficulty.  The initial value comes from the getter
workoutDifficultyController.title = @"Workout Difficulty";
[workoutDifficultyController setOriginalDifficulty:[self getWorkoutDifficulty]];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[(UINavigationController *)self.parentViewController pushViewController:workoutDifficultyController 
                                                               animated:YES];

//then in that workoutDifficultyController it calls back into the first controller to set the selected value:
[manageWorkoutController setWorkoutDifficulty:selectedDifficulty];
A: 

You have to retain workoutDifficulty whenever you set it to a new value (and release the old value).

Ole Begemann
+1  A: 

You've got many issues here. First, you're creating your accessors incorrectly. The problem that's particularly causing you trouble is this line:

workoutDifficulty = [NSString stringWithFormat:@"%d", value];

value is an NSString here. You should be receiving a warning about this. I believe "Typecheck Calls to printf/scanf" is turned on by default, and should catch this. workoutDifficulty is being set to some random number (probably taken from the first 4 bytes of value).

Here is what you probably meant. I would probably switch workoutDifficulty to an enum, but I'm keeping it an NSString for consistency with your code. I'm also doing this without properties because you did, but I would use a property here.

//header file
@implementation ManageWorkoutViewController : UIViewController {
    NSString *_workoutDifficulty;
}

-(void)setWorkoutDifficulty:(NSString *)value;
-(NSString *)workoutDifficulty;  // NOTE: Name change. "getWorkoutDifficulty" is incorrect.

@end

//implementation file
-(NSString *)workoutDifficulty {
    if (nil == workoutDifficulty) {
        _workoutDifficulty = [@"Easy" retain];
    }

    NSLog(@"getter: Returning workoutDifficulty as: %@", _workoutDifficulty);

    return _workoutDifficulty;  
} //end workoutDifficulty

-(void)setWorkoutDifficulty:(NSString *)value {
    [value retain];
    [_workoutDifficulty release];
    _workoutDifficulty = value;
    NSLog(@"setter: workoutDifficulty set as: %@", _workoutDifficulty);    
}//end setWorkoutDifficulty
Rob Napier