@property没有设置新值

时间:2009-11-09 13:47:46

标签: iphone objective-c iphone-sdk-3.0

我正在构建一个iphone应用程序,该应用程序使用UIViewControllers中包含的表视图推送到UINavigation控制器(非常类似于Contacts应用程序)。当您触摸特定的表格单元格时,它会将新的视图控制器推送到导航控制器上,以允许您从表格视图列表中选择一个值。当您选择并触摸“保存”时,它会将该视图从堆栈中弹出并移回第一个视图,原始表格视图应显示您选择的值。

问题是我将所选值存储在第一个视图控制器中的@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

3 个答案:

答案 0 :(得分:2)

您应该尝试将[self.tableView reloadData]添加到第一个控制器中的viewWillAppear(在其他两个语句之间)。

答案 1 :(得分:0)

这个怎么样......

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

答案 2 :(得分: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];
第一个控制器中的

应该足以让事情发挥作用

相关问题