0

I have a simple master-detail application and I'm using the detail view to configure data to insert into the master list.

I have a custom class called "Calculation," which is the object I need to insert into each row.

I can pass the object from the detail view to the master view just fine, it just won't insert a new row.

In the detail view I Initiate my object, populate it with data and send it to the "insertNewObject" method in the MasterViewController using a custom segue:

- (IBAction)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
    [f setNumberStyle:NSNumberFormatterDecimalStyle];
    NSString *s = @"1";
    NSNumber *n = [f numberFromString:s];

    Calculation *calc = [[Calculation alloc] initWithValue:@"$1661.70" descr:@"1 Ounce 24k Gold" textInput:@"1" percentDisplay:@"0%" purityIndex:n metalIndex:n weightUnitIndex:n sliderValue:n];

    [segue.destinationViewController insertNewObject:calc];
}

And in the MasterViewController, here is my insert code:

    -(void)insertNewObject:(NSObject *)newCalculation {

        if (!_objects) {
            _objects = [[NSMutableArray alloc] init];
        }
        [_objects insertObject:newCalculation atIndex:0];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

        [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    NSObject *object = [_objects objectAtIndex:indexPath.row];
    cell.textLabel.text = [object valueForKey:@"value"];
    cell.detailTextLabel.text = [object valueForKey:@"descr"];
    cell.textLabel.shadowColor = [UIColor blackColor];
    cell.textLabel.shadowOffset = CGSizeMake(0, -1);
    return cell;
}

I can print the object all day with NSLog in the MVC, but not matter how many times I add an object from the detail view controller, the array count is always 1, and no cell ever gets added to the tableview.

The same exact code works fine if I instantiate the object in MVC, and insert it.

1 Answer 1

0

A segue is really the wrong mechanism for sending data from detail to master. (It works great the other direction!) You'd be better off defining a delegate protocol that has -(void)insertNewObject:(NSObject *)newCalculation; as a required method, making your master implement that protocol, and passing it as a delegate to your detail controller.

Then the detail controller just calls [delegate insertNewObject:calc];.

3
  • Thanks Phillip, can you give me a little more guidance on how exactly to implement the delegate/protocol?
    – jhilgert00
    Apr 13, 2012 at 14:37
  • If you're not familiar with protocols, the easier way (though not as 'elegant') is to put a property called delegate in your detail controller that holds a reference to your master controller. In your original segue to the detail, have the master do something like detail.delegate = self;. Import the master's .h file into detail's .m and then call the method as above. Apr 13, 2012 at 14:42
  • Thanks again Phillip. I'm reading up on delegates and I'll mark your answer when I get it working. Having trouble at the moment.
    – jhilgert00
    Apr 13, 2012 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.