我正在構建一個iphone應用程序,該應用程序使用UIViewControllers中包含的表視圖,並將其推送到UINavigation控制器(非常類似於Contacts應用程序)。當你觸摸一個特定的表格單元格時,它將一個新的視圖控制器推到導航控制器上,讓你從表格視圖列表中選擇一個值。當您選擇並點擊「保存」時,它會將該視圖從堆棧中彈出,並將您移回第一個視圖,原始表格視圖應顯示您選擇的值。@property沒有設置新值
問題是,我將選定的值存儲在位於第一個視圖控制器中的@property中,並且它似乎沒有得到所選的值。這發生在「setDiff」方法中。我可以註銷它,它似乎已經設置,但它未設置視圖呈現後屬性已被更改。
這是第一個視圖控制器的代碼(其中第二個視圖控制器的選定值將在選定後顯示)。
/**
*
* ManageWorkoutViewController
*
**/
@class ManageWODiffController;
@interface ManageWorkoutViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
IBOutlet ManageWODiffController *workoutDifficultyController;
IBOutlet UITableView *woTableView;
IBOutlet UITableViewCell *workoutCommentsCell;
IBOutlet UITableViewCell *workoutDifficultyCell;
IBOutlet UITableViewCell *workoutDateCell;
NSString *workoutDifficulty;
NSString *workoutDate;
}
@property (nonatomic, retain) UITableView *woTableView;
@property (nonatomic, retain) NSString *workoutDifficulty;
@property (nonatomic, retain) NSString *workoutDate;
-(void)setupWorkoutAddEdit;
-(void)setDiff:(NSString *)value;
@end
#import "ManageWorkoutViewController.h"
@implementation ManageWorkoutViewController
@synthesize woTableView;
@synthesize workoutDifficulty;
@synthesize workoutDate;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (indexPath.row == 0) {
//workout comments
cell = [tableView dequeueReusableCellWithIdentifier:@"workoutCommentsCell"];
if (nil == cell) {
cell = workoutCommentsCell;
cell.selectionStyle = UITableViewCellStyleValue1;
}
}else if (indexPath.row == 1) {
//difficulty
cell = [tableView dequeueReusableCellWithIdentifier:@"workoutDifficultyCell"];
if (nil == cell) {
cell = workoutDifficultyCell;
cell.selectionStyle = UITableViewCellStyleValue1;
cell.textLabel.text = self.workoutDifficulty;
}
}else if (indexPath.row == 2) {
//workoutDate
cell = [tableView dequeueReusableCellWithIdentifier:@"workoutDateCell"];
if (nil == cell) {
cell = workoutDateCell;
cell.selectionStyle = UITableViewCellStyleValue1;
cell.textLabel.text = self.workoutDate;
}
}//end if-else
return cell;
}//end cellForRowAtIndexPath
- (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section {
return 3;
}//end numberOfRowsInSection
-(void)setDiff:(NSString *)value{
self.workoutDifficulty = value;
[woTableView reloadData];
NSLog(@"setter: workoutDifficulty set as: %@", self.workoutDifficulty);
}//end setDiff
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
case 0:
//do nothing no nav-view here
break;
//DIFFICULTY
case 1:
workoutDifficultyController.title = @"Workout Difficulty";
workoutDifficultyController.originalDifficulty = self.workoutDifficulty;//set the selected difficulty string
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[(UINavigationController *)self.parentViewController pushViewController:workoutDifficultyController
animated:YES];
break;
case 2:
workoutDateController.title = @"Workout Date";
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[(UINavigationController *)self.parentViewController pushViewController:workoutDateController
animated:YES];
break;
default:
break;
}//end switch
}//end didSelectRowAtIndexPath
- (void)viewWillAppear:(BOOL)animated {
//setup the UI to add/edit the workout
[self setupWorkoutAddEdit];
[super viewWillAppear:animated];
}//end viewWillAppear
-(void)setupWorkoutAddEdit{
//load the difficulty
if (nil == self.workoutDifficulty) {
switch ([[workout retrieveValueForKey:@"workoutDifficultyId"] intValue]) {
case 0:
self.workoutDifficulty = @"Easy";
break;
case 1:
self.workoutDifficulty = @"Medium";
break;
case 2:
self.workoutDifficulty = @"Hard";
break;
default:
break;
}
}//end if nil
NSLog(@"workoutDifficulty is: %@", self.workoutDifficulty);
}//end setupWorkoutAddEdit
@end
這裏是ManageWODiffController的代碼(其中值從表視圖中選擇並保存)。
/**
*
* ManageWODiffController
*
**/
@class ManageWorkoutViewController;
@interface ManageWODiffController : UIViewController <UITableViewDelegate> {
IBOutlet UITableView *tableView;
IBOutlet UITableViewCell *checkCell;
NSString *selectedDifficulty;
NSString *originalDifficulty;
IBOutlet ManageWorkoutViewController *manageWorkoutController;
}
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSString *selectedDifficulty;
@property (nonatomic, retain) NSString *originalDifficulty;
-(IBAction)cancelDifficulty:(id)sender;
-(IBAction)saveDifficulty:(id)sender;
@end
#import "ManageWODiffController.h"
#import "ManageWorkoutViewController.h"
@implementation ManageWODiffController
@synthesize tableView;
@synthesize selectedDifficulty;
@synthesize originalDifficulty;
-(IBAction)saveDifficulty:(id)sender {
NSLog(@"[ManageWODiffController.saveDifficulty] returning: %@", self.selectedDifficulty);
[manageWorkoutController setDiff: self.selectedDifficulty];
[(UINavigationController *)self.parentViewController popViewControllerAnimated:YES];
}//end saveDifficulty
-(IBAction)cancelDifficulty:(id)sender { /*...*/ }
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath { /*...*/ }
- (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section { /*...*/ }
@end
我嘗試這樣做,得到了與該錯誤的崩潰: - [CFString字符串isEqualToString:] :發送到釋放實例0x3b78110的消息。 (注意NSZombieEnabled打開)。不知道爲什麼因爲isEqualToString僅用於第二個視圖控制器的cellForRowAtIndexPath(在第一個視圖中無處) – bwizzy 2009-11-09 14:13:12
好像「self.tableView」引用了第二個視圖控制器的tableView。從粘貼的代碼中無法看出發生的原因... – 2009-11-09 14:21:29
在setupWorkoutAddEdit方法顯示值未更改後,記錄viewWillAppear中的值。 – bwizzy 2009-11-09 14:22:20