2009-11-03 65 views
0

我正在构建一个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]; 
+0

我想你的意思是谈论一个实例变量,而不是一个类变量。真的很难遵循你所说的话。如果你真的发布相关的代码,这真的很有帮助。 – 2009-11-03 14:04:13

+2

永远不要在你的getter前添加“get”。领先的“get”意味着Cocoa中的不同,这样做会打破KVC的许多部分依赖的KVC。 http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/KeyValueCoding/KeyValueCoding.html – 2009-11-03 14:31:20

回答

0

您必须保留workoutDifficulty只要你将它设置为一个新值(并释放旧值)。

1

这里有很多问题。首先,您错误地创建了访问者。那会特别的你造成麻烦的问题是这一行:

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

value这里是一个NSString。你应该收到关于这个的警告。我相信“Typecheck调用printf/scanf”是默认打开的,应该抓住这个。 workoutDifficulty正被设置为某个随机数(可能取自value的前4个字节)。

这是你可能的意思。我可能会将workoutDifficulty转换为一个枚举,但我将它保持为一个NSString,以便与代码保持一致。我也这样做没有属性,因为你做了,但我会在这里使用一个属性。

//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