Reputation: 21
I'm doing the iOS Apprentice Bundle and I'm not very good...
I was working through the tutorial when a "Done" button stopped working. It's attached to an IBAction that then calls self.delegate. I know this works as I added an NSLog but (after adding an NSLog at its destination) it's never reached. It's meant to add data the user inputted into a new table row.
Any help would be really appreciated! Thank you!
Here's the code:
ListDetailViewControllerTableViewController.h
#import <UIKit/UIKit.h>
@class ListDetailViewController;
@class Checklist;
@protocol ListDetailViewControllerDelegate <NSObject>
- (void)listDetailViewControllerDidCancel:(ListDetailViewController *)controller;
- (void)listDetailViewController:(ListDetailViewController *)controller didFinishAddingChecklist:(Checklist *)checklist;
- (void)listDetailViewController:(ListDetailViewController *)controller didFinishEditingChecklist:(Checklist *)checklist;
@end
@interface ListDetailViewController : UITableViewController <UITextFieldDelegate>
@property (nonatomic, strong) IBOutlet UITextField *textField;
@property (nonatomic, strong) IBOutlet UIBarButtonItem *doneBarButton;
@property (nonatomic, weak) id <ListDetailViewControllerDelegate> delegate;
@property (nonatomic, strong) Checklist *checklistToEdit;
- (IBAction)cancel;
- (IBAction)done;
@end
ListDetailViewControllerTableViewController.m
- (IBAction)done
{
if (self.checklistToEdit == nil) {
Checklist *checklist = [[Checklist alloc] init];
checklist.name = self.textField.text;
[self.delegate listDetailViewController:self didFinishAddingChecklist:checklist];
} else {
self.checklistToEdit.name = self.textField.text;
[self.delegate listDetailViewController:self didFinishEditingChecklist:self.checklistToEdit];
}
NSLog(@"Hello, I'm a 'Done' log");
}
AllListsViewControllerTableViewController.m
- (void)listDetailViewController:(ListDetailViewController *)controller didFinishAddingChecklist:(Checklist *)checklist
{
NSLog(@"Adding Checklist");
controller.delegate = self;
NSUInteger newRowIndex = [self.dataModel.lists count];
[self.dataModel.lists addObject:checklist];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
[self dismissViewControllerAnimated:YES completion:nil];
NSLog(@"finished adding checklist");
}
AllListsViewControllerTableViewController.h
#import <UIKit/UIKit.h>
#import "ListDetailViewControllerTableViewController.h"
#import "DataModel.h"
@interface AllListsViewController : UITableViewController <ListDetailViewControllerDelegate>
@property (nonatomic, strong) DataModel *dataModel;
@end
Thanks!
Upvotes: 1
Views: 53
Reputation: 104082
The problem is where you're setting the delegate -- you can't set it in the implementation of the delegate protocol method (listDetailViewController:didFinishAddingChecklist:), because that method is only called if the delegate is already set. You need to set the delegate when you create an instance of the ListDetailViewControllerTableViewController class.
Upvotes: 1