简体   繁体   中英

UITableView not updating after editor view controller dismissed

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. :)

 -(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];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM