delegate method is being ignored

Go To StackoverFlow.com

1

I know there are a few questions on stack alrdy regarding this, and I have been through them all.

When I debug, a line of code that calls a delegate method appears to be ignored. here's the line: [_delegate insertDataLocation:dbLocation Time:dbTime Reminder:dbReminder];

I am assuming it's a matter of the delegate not being set properly, so here's how i've set it: ViewController.h

@protocol mapDelegate;

@interface ViewController : UIViewController

@property (strong, nonatomic) id<mapDelegate> delegate;

ViewController.m

@synthesize delegate = _delegate;

- (void)viewDidLoad
{
[super viewDidLoad];
[self setDelegate:_delegate];
}

//Here's where I call the method, FYI
[_delegate insertDataLocation:dbLocation Time:dbTime Reminder:dbReminder];

AppDelegate.h

@protocol mapDelegate
-(void)insertDataLocation:(NSString*)l Time:(NSString*)t Reminder:(NSString*)r;
@end

@interface AppDelegate : UIResponder <UIApplicationDelegate, mapDelegate>

AppDelegate.m

-(void)insertDataLocation:(NSString*)l Time:(NSString*)t Reminder:(NSString*)r {
//Here's my method's code
}
2012-04-05 20:43
by Solid I
Shouldn't @protocol mapDelegate; conform to NSObject? Also, where are you setting your app delegate as the mapDelegate - CodaFi 2012-04-05 20:47
@CodaFi No, it doesn't - lnafziger 2012-04-05 20:49
You don't show the delegate being set in the code that you posted - lnafziger 2012-04-05 20:50
@Inafziger, then why does the standard Xcode delegate always include an NSObject conform - CodaFi 2012-04-05 20:51
@CodaFi They do for informal protocols. This isn't needed for formal protocols. See "The Objective-C Programming Language"'s Protocol section: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.htm - lnafziger 2012-04-05 21:06
@Inafziger, I see no reason to believe his protocol is formal in any way. Besides, without conforming to NSObject, his delegate object won't understand retain-release messages from the compiler - CodaFi 2012-04-05 21:07
@CodaFi Well, it is a formal protocol because of the way that he declared it. Informal protocols are categories of other objects (usually NSObject) but this is a stand-alone protocol and not a category - lnafziger 2012-04-05 21:10
@Inafziger, where have you seen standalone protocols without an NSObject conform? Even in the UIApplicationDelegate reference, there is no mention of it. Every delegate (excluding some in categories) must conform to NSObject like so: @protocol mapDelegate <NSObject>

And for the OP, @protocol is not terminated with a semi-colon - CodaFi 2012-04-05 21:12

@CodaFi Well, this works, and is well documented. Please look at the link that I posted above before continuing this discussion or ask your own question about it if you want more info. This isn't the right place to discuss this at length - lnafziger 2012-04-05 21:14


3

1) Get rid of the id<mapDelegate> delegate; declaration at the start of your .h file. You've tied your property to a variable called _delegate in your @synthesize statement, so the other one is misleading.

2) You say, "here's how i've set it," but I don't see anything that actually sets the delegate to be some object.

3) Using self.delegate rather than _delegate inside normal methods is usually a better idea.

2012-04-05 20:50
by Phillip Mills
The third is untrue. Setting and using a backing iVar is just as reliable as calling the property - CodaFi 2012-04-05 20:52
It is just as reliable if you're sure there's no custom getter/setter involved but I see no need to force myself to remember that bit of trivia for every property. : - Phillip Mills 2012-04-05 20:54
@CodaFi the leading underscore convention is to prevent you using the ivar all over the place by accident instead of the property. If you're going to use ivars, why uglify your code with underscores all over the place - jrturton 2012-04-05 21:11
Did anyone notice the semicolon after the @protocol declaration - CodaFi 2012-04-05 21:14
Noticed the semicolon...isn't that just a declaration that the protocol exists...sort of like @class ExternalClass;? (I still feel this is basically a "make the delegate be a real object" problem. - Phillip Mills 2012-04-05 21:17
Yes, it is. Forward declaration so that you don't get an error for the unknown protocol. It is then defined in his AppDelegate.h file - lnafziger 2012-04-05 21:17
+1, all three valid points. I suspect that he is setting the delegate with something like delegate = self; in which case he is assigning the delegate to the unused ivar, or isn't setting it at all.. - lnafziger 2012-04-05 21:19
I actually did set the deleagte in my viewDidLoad [self setDelegate:_delegate];, I just forgot to include that in the question - Solid I 2012-04-05 21:25
But where did you get _delegate from in order to set it as your current delegate?!? That looks way too self-referential. (My point is, that looks a lot like _delegate = _delegate; which does you no good. - Phillip Mills 2012-04-05 21:28
that line just sets the delegate to it's already existing value (nil) You probably want [self setDelegate:(id <mapDelegate>)[[UIApplication sharedApplication] delegate]]; to set it to your appDelegate (which is where your function is) - lnafziger 2012-04-05 21:30
@Inafziger That line of code worked. Now the delegate is set and the function is called from within the delegate. Thanks - Solid I 2012-04-06 00:18
Ads