2009-11-09 106 views
1

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

回答

2

你应该尽量在第一控制器添加[self.tableView reloadData]viewWillAppear(两者之间的其他语句)。

+0

我尝试这样做,得到了与该错误的崩溃: - [CFString字符串isEqualToString:] :发送到释放实例0x3b78110的消息。 (注意NSZombieEnabled打开)。不知道为什么因为isEqualToString仅用于第二个视图控制器的cellForRowAtIndexPath(在第一个视图中无处) – bwizzy 2009-11-09 14:13:12

+0

好像“self.tableView”引用了第二个视图控制器的tableView。从粘贴的代码中无法看出发生的原因... – 2009-11-09 14:21:29

+0

在setupWorkoutAddEdit方法显示值未更改后,记录viewWillAppear中的值。 – bwizzy 2009-11-09 14:22:20

0

这个怎么样...

... 
//difficulty 
cell = [tableView dequeueReusableCellWithIdentifier:@"workoutDifficultyCell"]; 
if (nil == cell) { 
    cell = workoutDifficultyCell; 
    cell.selectionStyle = UITableViewCellStyleValue1; 
} 
cell.textLabel.text = self.workoutDifficulty; 
... 
+0

self.workoutDifficulty的值仍未更新。我认为它与表格视图无关,因为如果我在viewWillAppear中记录该值,它将保持不变。 – bwizzy 2009-11-09 14:29:45

+0

另外,selectionStyle与单元格的风格不同。单元格初始化后,单元格的样式不能更改。选择样式定义单元格被选中时单元格是显示蓝色,灰色还是不变。 – 2009-11-09 15:31:34

0

与上下文对象的方法: 在你的第一控制器,该ManageWorkoutViewController,创造一个上下文对象

@property (nonatomic, retain) NSMutableDictonary *workout;

在该表中小区的情况下显示的难度应该从

[workout objectForKey:@"Difficulty"]; 

在第二个控制器(ManageWODiffController)中执行相同操作。

然后在第一个你去这样

//DIFFICULTY 
    case 1: 
     ManageWODiffController *diffController = [[ManageWODiffController alloc]  initWithNibName:@"ManageWODiffController" bundle:[NSBundle mainBundle]]; 
     diffController.workout = workout; 
     [[self navigationController] setNavigationBarHidden:NO animated:NO]; 
     [self.navigationController pushViewController:diffController animated:YES]; 
     [diffController release]; 
     diffController = nil; 
      break; 

在第二控制器应该像

-(IBAction)saveDifficulty:(id)sender 
{ 
    [workout setObject: selectDifficulty forKey:@"Difficulty"]; 
} 

然后把困难的情况下锻炼后弹出第二控制器。

[[self navigationController] popViewControllerAnimated:YES]; 

如果在第一控制器这样做的一个

[self.tableView reloadData]; 

应该足以使事情工作