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?
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:.AddViewControllerhas adelegateproperty like this:That just means that the delegate must conform to the named protocol. The delegate itself indicates that it does so in its interface:
Note that this is marked
assignfor reasons @Yuras explains. But if you’re writing new code targeted at iOS 5, you should useweakinstead ofassign.weakproperties are automatically set tonilif their referenced object is deallocated. It’s just safer that way. No dangling pointers.