2009-11-09 112 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]; 

應該足以使事情工作