我正在构建一个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