0

I'm a little new to iOS development. I have these methods declared and implemented in two separate view controllers (a Master view controller and an Editor view controller) and need to pass data through from the editor to the master. The master contains a table view that needs to update with the appropriate name. (Note: I used the default Master-Detail Application template for iPhone. I didn't manually create this with the Single-View Application)

Currently, the UITableView inside the MasterViewController doesn't update with a new cell. It's been such a hectic week for me, I must just be missing something. Here's the code for the Master View Controller:

@implementation FTMasterViewController
@synthesize bPlusMinusField, cPlusMinusField, cTerm, bTerm;

-(void) passTrinomial:(CCTrinomial *)tri withBPlusMinusField:(NSString *)bField withBTerm:(double)bTermField withCPlusMinusField:(NSString *)cField withCTerm:(double)cTermField {



    //allocate the array
    if (!self.trinomials) {
    self.trinomials = [[NSMutableArray alloc] init];
    }
    //set Master VC properties to tri fields passed in
    self.bPlusMinusField = bField;
    self.cPlusMinusField = cField;
    self.bTerm = bTermField;
    self.cTerm = cTermField;
    NSString *trinomial = [NSString stringWithFormat:@"x² %@ %g %@ %g", bPlusMinusField, bTerm, cPlusMinusField, cTerm];

    [self.trinomials insertObject:trinomial atIndex:0];

    NSLog(@"%lu", (unsigned long)[self.trinomials count]);

}

#pragma mark - Table View

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.trinomials count];
}

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

    cell.textLabel.text = [NSString stringWithFormat:@"x² %@ %g %@ %g", bPlusMinusField, bTerm, cPlusMinusField, cTerm];
    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSDate *object = _objects[indexPath.row];
        [[segue destinationViewController] setDetailItem:object];
    }
}



@end

This is the code that I deemed necessary to see to fix the problem: obviously, I have the default overridden methods in the rest of the implementation.

This is the Editor View Controller code. When the thought appears for you, yes, the "done" button is linked to the -handleDoneButtonPushed: method in my Main Storyboard.

-(IBAction)handleDoneButtonPushed:(id)sender {
    [self dismissViewControllerAnimated:YES completion:NULL];
    FTMasterViewController *masterVC = [[FTMasterViewController alloc] init];
    [masterVC passTrinomial:factoring withBPlusMinusField:bPlusMinusField.text withBTerm:[bTriField.text doubleValue] withCPlusMinusField:cPlusMinusField.text withCTerm:[cTriField.text doubleValue]];


}

This editor view controller simply gets some values in the form of a UITextField and hands them off to the Master View Controller through an object of that class. Again, when I click the Done button on my top nav-bar, it dismisses the Editor View Controller but doesn't update the table view with those values retrieved.

Help would be greatly appreciated. :)

Hunter E.
  • 108
  • 9

1 Answers1

0
-(IBAction)handleDoneButtonPushed:(id)sender 
{
    [self dismissViewControllerAnimated:YES completion:NULL];
    FTMasterViewController *masterVC = [[FTMasterViewController alloc] init];
    [masterVC passTrinomial:factoring 
        withBPlusMinusField:bPlusMinusField.text 
                  withBTerm:[bTriField.text doubleValue]  
        withCPlusMinusField:cPlusMinusField.text 
                  withCTerm:[cTriField.text doubleValue]];

}

This is wrong, you already has an instance of MasterViewController you should not instantiate a new one, pass the data and expect the previous one to update the detailed value.

You should use the delegation pattern.

Create a Protocol in FTEditorController. A detailed post on implementing Delegates in Objective-C

@protocol FTEditorControllerDelegate <NSObject>

@optional
- (void)editorController:(FTEditorViewController *)editorController didCompleteEditingTrinomial:(NSString *)trinomial;

@end

When you set MasterController as delegate of EditorController, once an event of interest occurs in EditorController, it gives that event to its delegate.

So once you are done with creating trinomial expression pass it to MasterController.

    -(IBAction)handleDoneButtonPushed:(id)sender 
    {
        [self dismissViewControllerAnimated:YES completion:NULL];
        NSString *trinomial = [NSString stringWithFormat:@"x² %@ %g %@ %g", bPlusMinusField.text, [bTriField.text doubleValue], cPlusMinusField.text, [cTriField.text doubleValue]];
        if([self.delegate respondsToSelector:@selector(editorController:didCompleteEditingTrinomial:)])
       {
            [self.delegate editorController:self didCompleteEditingTrinomial:trinomial];
       }

   }


//FTEditorControllerDelegate implementation in MasterViewController
- (void)editorController:(FTEditorViewController *)editorController didCompleteEditingTrinomial:(NSString *)trinomial{

    //allocate the array
    if (!self.trinomials) {
    self.trinomials = [[NSMutableArray alloc] init];
    }

    //This condition is tricky, I assume you always want to display the edited
    //trinomial expression as the first one in tableView
    [self.trinomials insertObject:trinomial atIndex:0];

    //Reloads the tableView to reflect the changes 
    [self.tableView reloadData];

}
Community
  • 1
  • 1
Anupdas
  • 10,058
  • 2
  • 33
  • 59
  • A-hah! I knew there was something I was missing. I had used that delegation pattern before, but I was thinking the wrong way in how I used the method. Let me try it out and see how it works. – Hunter E. Mar 09 '13 at 19:17
  • @HunterE. Glad to help you. The insertion of trinomial is something you need to work on. – Anupdas Mar 09 '13 at 19:22
  • Well, under the -handleDoneButtonPushed: method, I did this: [self.delegate editorController:self didCompleteEditingTrinomial:trinomialString] and then adopted the protocol and implemented the method in FTMasterViewController. I simply pasted the code right into the implementation, but the table view is still not updating. I've revised the -tableView:cellForRowAtIndexPath: to handle the array, but still doesn't. Thoughts? – Hunter E. Mar 09 '13 at 19:37
  • @HunterE. Did you set MasterViewController as the delegate of EditorViewController prepareForSegue: method? – Anupdas Mar 09 '13 at 19:44
  • There's the thing: it's not a segue from the Editor View Controller to the Master View Controller. It's dismissed programmatically. I couldn't create a segue from the done button to the Master View Controller because if done, it wouldn't load the Navigation Controller (the root view controller) and if I had the segue to the Nav Controller, I wouldn't be able to pass in that data. I don't have a place to set the delegate. (and, as a refresher, how might I set the delegate?) – Hunter E. Mar 09 '13 at 19:46
  • @HunterE. I was mentioning about the segue of Master to Editor. There is the only point to set delegate Master as delegate of Editor rite. And also i have updated the handleDoneButtonPushed. – Anupdas Mar 09 '13 at 19:59
  • Oh, ok. So what is the code I put in the prepareForSegue: method? In between the "if([[segue identifier] isEqualToString:@"editorView"]) { ... }"? – Hunter E. Mar 09 '13 at 20:04
  • Got it! [[segue destinationViewController] setDelegate:self], and now it works! Thanks! – Hunter E. Mar 09 '13 at 20:06
  • @HunterE. Glad to hear that :). Now you may accept the answer. – Anupdas Mar 09 '13 at 20:07