开发者

Odd problem loading data into a text view field

BRIEF: Hi everyone, painfully working through my first app and couldn't find a solution online for this "loading" issue.

ATTEMPTING TO DO: (a)From a table view (b)click on a cell (c)push into a detail view where a text view field is populated from a property list file.

PROBLEM: Everything works (Nav bar title updates, backbutton title changes correctly) but the text view field comes up blank (default state from IB)... at first. If I click the back button, then back into the detail view, the text view then loads correctly and stays that way. Totally baffled.

CODE:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // check for view controller
    if (self.savedScriptDetailVC ==nil) {
            SavedScriptDetailVC *savedScriptDetail =[[SavedScriptDetailVC alloc]initWithNibName:@"SavedScriptDetailView" bundle:nil];
            self.savedScriptDetailVC = savedScriptDetail;
            [savedScriptDetail release];
    }
    //      find row
    NSInteger row = [indexPath row];

    //get filepaths and set up arrays for pulling project names and text data
    NSString *textPath = [self savedScriptsPath]; //using outside function to get path
    NSArray *array = [self savedProjectNames]; //using outside function to pull project names
    NSArray *textArray =[[NSArray alloc] initWithContentsOfFile:textPath];
    textArray = [textArray valueForKey:@"ScriptText"];

    //populate text for textview in detail view. Works, but have to drill down, back out before effect takes, why?-->
    savedScriptDetailVC.fScriptText.text = [NSString stringWithFormat:@"%@", [textArray objectAtIndex:row]];
    savedScriptDetailVC.title =[NSString stringWithFormat:@"%@", [array objectAtIndex:row]]; //change nav title

    //change back button text
    UIBarButtonItem *backButton =[[UIBarButtonItem alloc]initWithTitle:@"开发者_开发百科Scripts" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem =backButton;
    [backButton release];

    //using delegate to push views
    ProjectAppDelegate *delegate =[[UIApplication sharedApplication]delegate];
    [delegate.savedScriptsNavC pushViewController:savedScriptDetailVC animated:YES];
}


The actual view (and all its subviews) for a view controller is not loaded until it is needed, i.e. the view property is accessed. So at the point that you first try to set the value of the text field, the text field doesn't exist yet because none of the views in the view controller have been created yet. If you were to check, you would find that savedScriptDetailVC.fScriptText is nil.

A simple fix is to access [savedScriptDetailVC view] before trying to manipulate those subviews.


I agree with Anomie's explanation: the first time you try to set the text of the text field, the text field is nil so nothing happens. I recommend a different solution, however. Give SavedScriptDetailVC one or more properties where you can store whatever information it needs to do its job, and set those properties before you push the controller onto the navigation stack. Let SavedScriptDetailVC be responsible for properly configuring the views that it manages, probably in its -viewWillAppear method.

In general, the only object that should manipulate a view controller's views is that view controller. If you adopt that philosophy you'll completely avoid the kind of problem you're having now.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜