1

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:@selector(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?

Chris Kimpton
  • 5,546
  • 6
  • 45
  • 72
adrian
  • 4,574
  • 17
  • 68
  • 119

3 Answers3

3

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.

  • what's the solution for this? – adrian Oct 12 '11 at 10:20
  • We never call the -drawRect: method manually. Instead, we can call the - (void)setNeedsDisplay instance method of an UIView. It will request the system to redraw its view content. For more information, refer to the method description here http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html –  Oct 12 '11 at 10:26
  • Yeah, He isn't. That was only an indication to use -setNeedsDisplay method. People might always think as - why not lets call the -drawRect: method instead !. –  Oct 12 '11 at 10:40
  • this bis my whole code of the method that adopts the protocol http://pastebin.com/aDyhYjAX tell me what to do...please:) – adrian Oct 12 '11 at 10:43
  • @george: The issue looks a bit weird. Did you try [routeOverlayMapView.view setNeedsDisplay]; ? Alternatively the idea is to set the delegate before -drawRect gets called. You can implement a custom initializer method something as -initWithNibName:bundle:delegate: And in this method instantiate the controller by calling initWithNibName:bundle on super and then set the delegate. Thus, we are setting the delegate while initializing the nib itself. –  Oct 12 '11 at 10:48
1

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.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • you wre true...I was already pointing to that.I removed that line and replaced it with this: routeOverlayView.delegate = self; but is still not entering the if statement! – adrian Oct 12 '11 at 10:52
  • I'm not sure you need the if statement (see my update) but what is the value of delegate now when you are in `drawRect`? – jrturton Oct 12 '11 at 11:00
1

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];
    }
}
Dhaval
  • 168
  • 2
  • 12