2009-11-03 67 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