我正在构建一个iphone应用程序,并且我有一个单元格内的文本框的表视图,字段的内容设置在viewWillAppear(它的分组TableView w/3总是相同的字段)。文本字段的内容从getter方法中获取,该方法返回来自各个类变量的值。可能的视图缓存问题?
我遇到的问题是getter似乎正在返回原始值,而不是由setter方法修改的值。类变量是一个NSMutableString。视图缓存方法调用有可能吗?
//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];
我想你的意思是谈论一个实例变量,而不是一个类变量。真的很难遵循你所说的话。如果你真的发布相关的代码,这真的很有帮助。 – 2009-11-03 14:04:13
永远不要在你的getter前添加“get”。领先的“get”意味着Cocoa中的不同,这样做会打破KVC的许多部分依赖的KVC。 http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/KeyValueCoding/KeyValueCoding.html – 2009-11-03 14:31:20