views:

17

answers:

0

So I have a working diary app but with one problem. The UIViewTable is populated by Core Data and everything is good. But when I choose a row for the first time and open the detailView it is empty, if I back up and click again it is populated with Core Data info.

I have narrowed the problem down to one method in RootViewController and the DetailViewController.

Here is the method in RootViewController:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSInteger row = [indexPath row];
NSLog(@"row = %i", row);
Diary *diaryDetailRead = [diaryArray objectAtIndex:row];


if (self.diaryViewController == nil) {
    DiaryViewController *newDiaryViewController = [[DiaryViewController alloc]
                              initWithNibName:@"DiaryView"
                              bundle:[NSBundle mainBundle]];
    self.diaryViewController = newDiaryViewController;
    [newDiaryViewController release];
}

[self.diaryViewController initWithObject:diaryDetailRead];

[self.navigationController pushViewController:self.diaryViewController animated:YES];

}

And here is the two methods in DetailViewController:


   -(id) initWithObject: (Diary *) diaryDetailRead {
    //Plocka värderna ur DataCore-objektet
    labelDiaryTitle.text = diaryDetailRead.diaryTitle;
    labelDiaryContext.text = diaryDetailRead.diaryContext;

    //Konvertera NSDate till NSString
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *stringDate = [formatter stringFromDate:diaryDetailRead.diaryDate];
    [formatter release];

    labelDiaryDate.text = stringDate;

    [self setTitle: diaryDetailRead.diaryTitle];

    return self;
}

- (void)viewDidLoad {
    NSLog(@"viewDidLoad: %@", diaryDetailRead);
    labelDiaryTitle.text = diaryDetailRead.diaryTitle;
    labelDiaryContext.text = diaryDetailRead.diaryContext;

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *stringDate = [formatter stringFromDate:diaryDetailRead.diaryDate];
    [formatter release];

    labelDiaryDate.text = stringDate;

    [self setTitle: diaryDetailRead.diaryTitle];

    [super viewDidLoad];
}

In viewDidLoad the variable "diaryDetailRead" is undeclared, which is it not in the "initWithObject" method. The object get sent to the DetailViewController but I have failed to get the viewDidLoad to recive it correctly.

I guess that I have to make "diaryDetailRead" an instance varible, but I fail in making that an instance variable. I have tried but failed.

Please someone help me with this easy problem. I think it is... I need more Objective-C knowledge to slow this. But I need a quick fix. :D