0

In PSPoemsViewController.h I have a protocol declared as:

@protocol PSPoemsViewControllerDelegate <NSObject>

- (void) savePoem:(Poem *)thePoem withText:(PSTextStorage *)theText;

@end

and in the .m file I have its declaration:

- (void) savePoem:(Poem *)thePoem withText:(PSTextStorage *)theText;
{
...
}

In PSVersionViewController.h I have:

@interface PSVersionViewController : UIPageViewController <PSPoemsViewControllerDelegate, UIPageViewControllerDataSource>

When I compile this the compiler throws the warning:

/Users/Keith/Documents/My Projects/Poem Shed/Poem Shed/PSVersionViewController.m:28:17: Method 'savePoem:withText:' in protocol 'PSPoemsViewControllerDelegate' not implemented

The thing is the code executes and finds the method that is supposed to be missing. BUT there is a weird problem - it sometimes breaks on the method even though there is no break set, and then it crashes. This suggests some sort of corruption. I've tried doing a clean build, removing the method and putting it back, but nothing shifts the warning. I am using Xcode 6.1 (it also failed on beta copies).

easiwriter
  • 73
  • 1
  • 8
  • You don't declare the delegate method in the PSPoemsViewController ... You just invoke it... – CW0007007 Oct 28 '14 at 13:05
  • Surely the method needs to be invoked from the delegate (PSVersionViewController) and declared in the main class (PSPoemsViewController) that also declares the protocol. – easiwriter Oct 28 '14 at 13:12
  • Well you invoke it but you don't implement it like you have above.. see my answer below. – CW0007007 Oct 28 '14 at 13:16
  • Also see my answer here: http://stackoverflow.com/questions/21331523/how-to-declare-events-and-delegates-in-objective-c/21331771#21331771 – CW0007007 Oct 28 '14 at 13:19

1 Answers1

0

So:

@protocol PSPoemsViewControllerDelegate <NSObject>

- (void) savePoem:(Poem *)thePoem withText:(PSTextStorage *)theText;

@end

@interface PSPOemsViewController : UIViewController 

@property (weak) id < PSPoemsViewControllerDelegate> delegate;

@end

Then in the PSPoemsViewController.m when you want to invoke this method you call:

//Check that the delegate is set and that it has implemented the method. 
if ([self.delegate respondsToSelector:@selector(savePoem:withText:)])
    [self.delegate savePoem:YOUR_POEM withText:@"];

Then in the class that conforms to this delegate you add the method. So in your PSVersionViewController. Make sure you set the delegate too.

PSPoemsViewController *anInstanceofPoemsViewController = .....
[anIntanceOfPoemsViewController setDelegate:self];

Then add the method:

- (void) savePoem:(Poem *)thePoem withText:(PSTextStorage *)theText;
{
...
}
CW0007007
  • 5,681
  • 4
  • 26
  • 31