3

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?

Solid I
  • 580
  • 5
  • 13
  • 34
  • Are you sure your `AddEventViewController` actually has `delegate` property? Please post it's declaration. – Yuras Jan 30 '12 at 22:33

2 Answers2

4

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.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
3

In Objective-C any property you are using should be declared somewhere. Any properties, that are declared in parent class (UIViewController in your case) are inherited by all the derived classes (AddEventViewController in your case).

AddEventViewController inherits UIViewController, but the delegate property is not declared neither in the first, nor in the second. That is why compiler is not happy.

You should declare it. Something like the next:

@interface AddEventViewController : UIViewController

@property (nonatomic, assign) id delegate;  

@end

@implementation AddEventViewController

@synthesize delegate;

@end

Delegates are usually declared with assign attribute to prevent circle retains (e.g. A retains B and B retains A)

Yuras
  • 13,856
  • 1
  • 45
  • 58
  • 1
    Also it could be a good idea to read header files related to `UITableViewController`, `UITableViewDataSource` and `UITableViewDelegate` from iOS SDK to take an idea how to design classes with delegates. – Yuras Jan 30 '12 at 23:12
  • 1
    Thanks for explaining this. Now I've got to go back and really understand what the delegate is. I definitely "missed" that lesson over the past month. – Solid I Jan 30 '12 at 23:51