Property 'delegate' not found on object 'AddEventViewController'

Solid I picture Solid I · Jan 30, 2012 · Viewed 9.7k times · Source

With the help of the Developer Library, I am trying to work with the EventKit and EventKitUI Frameworks. I've hit a very early roadblock. I have copied and pasted code from the library found here. I have added a view controller called 'AddEventViewController' a button to the Navigation Bar in my ViewController and I am using this code to invoke it.

- (IBAction)add:(id)sender {
AddEventViewController *addController = [[AddEventViewController alloc]
                                          init];
addController.delegate = self;
UINavigationController *navigationController = [[UINavigationController alloc]
                                                    initWithRootViewController:addController];
[self presentViewController:navigationController animated:YES completion: nil];

}

The error shows on line: addController.delegate = self;

This code is copied straight from the Library. I am using Xcode 4.2 and a Storyboard if that might help.

UPDATE: This is AddEventViewController.h:

#import <UIKit/UIKit.h>

@interface AddEventViewController : UIViewController

@end

You're going to tell me I created this ViewController incorrectly? Please explain why just not "how" if you'd be so nice?

Answer

Rob Napier picture Rob Napier · Jan 31, 2012

I see how Apple's example here has likely confused you here. First, download the full source for iPhoneCoreDataRecipes (or at least reference it while trying to understand this code).

To really understand what's going on here, you need to read on down to the section called "Dismissing a Presented View Controller" and then follow the link to "Use a Delegation to Communicate With Other Controllers." ("a Delegation?" Very strange....)

So here's what's going on. The presented view has a "delegate" which is the object it's supposed to tell "interesting" things to. In this case, "interesting" things are "hey, I added a recipe!" To achieve this, the delegate implements a protocol, which just means it promises to implement some methods. In this case, the required method is recipeAddViewController:didAddRecipe:.

AddViewController has a delegate property like this:

@property(nonatomic, assign) id <RecipeAddDelegate> delegate;

That just means that the delegate must conform to the named protocol. The delegate itself indicates that it does so in its interface:

@interface RecipeListTableViewController : UITableViewController <RecipeAddDelegate, NSFetchedResultsControllerDelegate> {

Note that this is marked assign for reasons @Yuras explains. But if you're writing new code targeted at iOS 5, you should use weak instead of assign. weak properties are automatically set to nil if their referenced object is deallocated. It's just safer that way. No dangling pointers.