method delegates doesn't get called
I tried to implement this:
UICRouteOverlayMapView
.h file
@protocol DrawingDataDelegate <NSObject>
@required
-(void) drawingSuccessful:(BOOL)done;
@end
@interface UICRouteOverlayMapView : UIView {
id <DrawingDataDelegate> delegate;
}
- (id)initWithMapView:(MKMapView *)mapView;
@property (nonatomic, retain) id <DrawingDataDelegate> delegate;
@end
.m file
@implementation UICRouteOverlayMapView
@synthesize delegate;
- (void)drawRect:(CGRect)rect {
NSLog(@"mesagge");
if ([self.delegate respondsToSelector:@sele开发者_运维问答ctor(drawingSuccessful:)]) {
[self.delegate drawingSuccessful:YES];
}
}
The class that adopts the protocol:
.h file
#import "UICRouteOverlayMapView.h"
@class UICRouteOverlayMapView;
@interface ItineraireViewController : UIViewController <MKMapViewDelegate, UICGDirectionsDelegate, CLLocationManagerDelegate,
DrawingDataDelegate> {
UICRouteOverlayMapView *routeOverlayMapView;
}
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
routeOverlayMapView = [[UICRouteOverlayMapView alloc] init];
routeOverlayMapView.delegate = self;
}
-(void) drawingSuccessful:(BOOL)done{
NSLog(@"it's done");
}
Now, what am I doing wrong cause the method drawingSuccessful
never gets called?
I know for sure that the method
- (void)drawRect:(CGRect)rect {
NSLog(@"mesagge");
if ([self.delegate respondsToSelector:@selector(drawingSuccessful:)]) {
[self.delegate drawingSuccessful:YES];
}
}
is called because this gets displayed NSLog(@"mesagge");
.Please help
I did debug and set breakpoint at this line:
if ([self.delegate respondsToSelector:@selector(drawingSuccessful:)])
and I noticed that this is not a valid condition...it never enters the brackets...so this
it is not compiled [self.delegate drawingSuccessful:YES];
.
So, what is wrong?
That is happening probably because when you allocate the UICRouteOverlayMapView
, the -drawRect:
method gets called. But the delegate is set after that line. Hence, the delegate never receives the message.
Overall, the delegate should be nil at that time. Check if the delegate is nil.
Does the route overlay map view already appear in your nib? In your view didLoad you are creating a new instance of it, setting it's delegate, and then...nothing. You would normally be adding it to your subview, unless, as I say, it already exists in your nib file.
If it does, either set an outlet in UICRouteOverlayMapView and connect the delegate in interface builder, or within your viewDidLoad, set the delegate on whatever instance variable you are using to represent the actual map view.
It may just be a matter of removing this line:
routeOverlayMapView = [[UICRouteOverlayMapView alloc] init];
If routeOverlayMapView is already pointing at your real view.
You are probably not entering that last if statement because your delegate
is nil. The statement itself is redundant anyway since the method is required in your protocol.
george
Replace this method, I am sure that you get solution.
- (void)drawRect:(CGRect)rect
{
NSLog(@"mesagge");
if ([self.delegate respondsToSelector:@selector(drawingSuccessful:)]) {
[delegate drawingSuccessful:YES];
}
}
精彩评论