I'm new to delegation, so I've been looking for answers, and I've looked at method delegates doesn't get called and Delegate method not being called to see if my answer would be there. I've also looked at How do I set up a simple delegate to communicate between two view controllers? for a brief rundown of delegates and have done my best to follow it correctly. I have been checking my code and I thought I doing it right, but in my debugging, I've learned that I'm clearly not.
What I want to do is reload a tableView from the file "Data.plist" (stored in the Documents directory) when a user segues back to the parent ViewController (the user has just added a new value to the dictionary). The app loads the new entered correctly upon reopening, so I know that file is being saved correctly.
This is my ParentViewController.h file.
#import <UIKit/UIKit.h>
#import "ChildViewController.h"
@interface ParentViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource, ChildViewControllerDelegate>
@end
This is my ParentViewController.m file.
#import "ParentViewController.h"
@interface ParentViewController ()
@end
@implementation ParentViewController
-(void)addEvent {
NSLog(@"We are before [self loadNewEvent];");
[self loadNewEvent];
NSLog(@"We are after [self loadNewEvent];");
}
-(void)loadNewEvent {
[self.tableView scrollToRowAtIndexPath:0 atScrollPosition:UITableViewScrollPositionNone animated:YES];
[self.tableView reloadRowsAtIndexPaths:0 withRowAnimation:UITableViewRowAnimationLeft];
//[self.tableView reloadData];//This doesn't seem to do anything but leave the tableview blank when this line is in viewDidLoad and viewWillAppear.
NSLog(@"WHY IS THIS NOT WORKING?!?!");
}
- (void)viewDidLoad
{
[super viewDidLoad];
//stuff that loads data initially into tableView
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//cell formatting stuff
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"addEvent"]) {
[(ChildViewController *)[[segue destinationViewController] topViewController] setFilePath:filePath];
ChildViewController *detailViewController = [[ChildViewController alloc] init];
[detailViewController setDelegate:self];
//[self.navigationController pushViewController:detailViewController animated:YES];
}
}
@end
This is my ChildViewController.h
#import <UIKit/UIKit.h>
@protocol ChildViewControllerDelegate;
@interface ChildViewController : UIViewController
@property (nonatomic, weak) id<ChildViewControllerDelegate> delegate;
- (IBAction)dismissAndSave:(id)sender;//in implementation
- (IBAction)dismissCancel:(id)sender;//in implementation
@end
@protocol ChildViewControllerDelegate <NSObject>
-(void)addEvent;
@end
and finally my ChildViewController.m file.
#import "ChildViewController.h"
@interface ChildViewController ()
@end
@implementation ChildViewController
- (IBAction)dismissAndSave:(id)sender {
//perform file-saving operations
if ([self.delegate respondsToSelector:@selector(addEvent)]) {
[self.delegate addEvent];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
I've run through and debugged, and the last point where the ChildViewController is not nil is at the [detailViewController setDelegate:self];
line in the parent's prepareForSegue method.
The reason I knew my delegate methods weren't being called was because NSLog(@"We are before [self loadNewEvent];");
was not happening.
In addition to the method not being called, when I try to do a [self.tableView reloadData];
in the addEvent
delegate method, everything appears blank.
Thank you in advance for your help; you are doing a great service to a student who is teaching himself the ways of iOS programming and Objective-C!