简体   繁体   中英

Objective-C - Reloading table view data after dismissing modal

I am trying to reload my table view controller after inserting an item using core data. I am using storyboards to present my modal view controller and am setting the delegate of the destination view controller in prepare to segue but i believe that is the issue, as my didAddCollection method is not being triggered. I am not sure what i am missing here. i used iOS TableView Reload after dismissing modal as the base for the current code i have.

In my CCNewCollectionViewController.h

@protocol NewCollectionDelegate <NSObject>

-(void) didAddCollection;

@end


@interface CCNewCollectionViewController : UIViewController {
    id delegate;
}

@property (nonatomic, retain) id <NewCollectionDelegate> delegate;

In my CCNewCollectionViewController.m

@implementation CCNewCollectionViewController

@synthesize delegate;

// save data

[self.delegate didAddCollection];

// dismiss view controller

In my CollectionTableViewController.m

@interface CollectionTableViewController () <NSFetchedResultsControllerDelegate, NewCollectionDelegate>

-(void) didAddCollection {

    [self.tableView reloadData];

}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    [[segue destinationViewController] setDelegate:self];
}

A very silly mistake. When setting the delegate of the destination view controller, i was actually setting the delegate of the UINavigationController instead of the CCNewCollectionViewController.

UINavigationController *nc = [segue destinationViewController];
CCNewCollectionViewController *vc = nc.viewControllers[0];
[vc setDelegate:self];

I changed my prepareForSegue code to this and it solved my problems!

Not sure if this will solve your problem, but I think it is more typical to just create a weak delegate property. No need to synthesize.

    @interface CCNewCollectionViewController : UIViewController
    @property (nonatomic,weak) id <NewCollectionDelegate> delegate;
    ...
    @end

See here for more information about working with protocols.

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