开发者

dismissModalViewControllerAnimated crashes app

I am new to iPhone development, so please pardon my ignorance.

I call a UIViewController from a UIViewController. The first UIViewController is a list of items, and the second is the detail for each of the items.

ListViewController (first UIViewController):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // initialize a AddViewController
    selItem = indexPath.row;
    selIndexPath = indexPath;
    AddViewController *controller = [[AddViewController alloc] init];
    // give controller the data to display

    // show the AddViewController
    [controller setData:[list objectAtIndex:indexPath.row]];
    controller.delegate = self;
    [self presentModalViewController:controller animated:YES];
    [controller release]; // release the controller AddViewController
} // end method tableView:didSelectRowAtIndexPath:

In the 'AddViewController' I have a segment control as both the left and right button bars. On the left, I have 'Exit' and 'Delete'. On the right I have 'Add' and 'Save'. The outlets that the segment controls are linked to are:

AddViewController (second UIViewController):

- (IBAction)delExitSegment:sender
{
    // The segmented control was clicked, handle it here 
    UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
    switch (segmentedControl.selectedSegmentIndex) {
        case 0:     // Exit Button
            [delegate addViewControllerDidExit:self];
            break;

        case 1:     // Delete Button
            [delegate addViewControllerDelItem:self];
            break;

    }

}

- (IBAction)segmentAction:sender
{
    // The segmented control was clicked, handle it here 
    UISegmentedControl *segCtl = (UISegmentedControl *)sender;
    switch (segCtl.selectedSegmentIndex) {
        case 0:     // Add Button
            if (currentCell != nil)
                [data setValue:currentCell.textField.text forKey:currentCell.label.text];

            [delegate addViewControllerDidFinishAdding:self];
            break;

        case 1:     // Save Button
            [delegate addViewControllerUpdate:self];
            break;

    }
}

When I come back to my first view controller, I have: ListViewController:

- (void)addViewControllerDidFinishAdding:(AddViewController *)controller
{
    NSDictionary *item = [controller values];
    if (item != nil)
    {
        [list addObject:item];
    }
    [self dismissModalViewControllerAnimated:YES];
    [list writeToFile:itemFilePath atomically:NO];
    [self calcTotal];
    [self.tableView reloadData];
}

- (void)addViewControllerUpdate:(AddViewController *)controller
{
    NSDictionary *item = [controller values];
    if (item != nil)
    {
        [list replaceObjectAtIndex:selItem withObject:item];
    }
    [self dismissModalViewControllerAnimated:YES];
    [list writeToFile:itemFilePath atomically:NO];
    [self calcTotal];
    [self.tableView reloadData];
}

- (void)addViewControllerDidExit:(AddViewController *)controller
{
    [self dismissModalViewControllerAnimated:YES];
    [self.tableView reloadData];
}

- (void)addViewControllerDelItem:(AddViewController *)controller
{
    NSDictionary *item = [controller values];
    if (item != nil)
    {
        [list removeObjectAtIndex:selItem];
                [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:selIndexPath] withRowAnimation:UITableViewRowAnimationFade];
        [list writeToFile:itemFilePath atomically:NO];
    }
    [self dismissModalViewControllerAnimated:YES];
    [self.tableView reloadData];
}

If I exit or delete an item from AddViewController, I have no problems. When I try and add or save, then my application will crash. The debugger crashes at dismissModalViewControllerAnimated. I can't see what's different between the two segment controls. Any ideas what I may have wrong?

I just discovered that neither the left or right buttons work when I attempt to edit a cell. The editing 开发者_开发技巧cell in AddViewController is as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"EditableCell";

    EditableCell *cell = (EditableCell *)[table dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil)
    {
        cell = [[EditableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    NSString *key = [fields objectAtIndex:indexPath.row + indexPath.section * 3];
    [cell setLabelText:key];

    cell.textField.text = [data valueForKey:key];

    if (indexPath.section == 0 && (indexPath.row == 1 || indexPath.row == 3))
        cell.textField.keyboardType = UIKeyboardTypeNumberPad;
    if (indexPath.section == 1)
        cell.textField.keyboardType = UIKeyboardTypeNumberPad;

    cell.editing = NO;
    cell.delegate = self;

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;

}


Here is the problem:

[self dismissModalViewControllerAnimated:YES];
[self.tableView reloadData];

When you dismiss the view the value of self become unknown, accessing the tableview will definitely crash.

If you want to refresh the tableView just use [self.tableView reloadData] and don't dismiss the view.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜