我正在構建一個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