方法代表不会被调用 [英] method delegates doesn't get called

查看:69
本文介绍了方法代表不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现这一点:

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];
        }
    }

采用该协议的类:

.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");
}

现在,我在做错什么导致方法 drawingSuccessful 不会被调用?

Now, what am I doing wrong cause the method drawingSuccessful never gets called?

我确定方法

- (void)drawRect:(CGRect)rect {
         NSLog(@"mesagge");
        if ([self.delegate respondsToSelector:@selector(drawingSuccessful:)]) {
            [self.delegate drawingSuccessful:YES];
        }
    }

因为它被显示为 NSLog(@ mesagge); 。请帮助

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:)])

,我注意到这不是有效条件...它永远不会进入括号...因此,此
不会被编译 [self.delegate drawingSuccessful:YES];
那么,怎么了?

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?

推荐答案

路径叠加图视图是否已出现在您的笔尖中?在您的didLoad视图中,您正在创建它的新实例,设置它的委托,然后什么都没有。通常,您将其添加到子视图中,除非我说过,它已存在于您的nib文件中。

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.

如果这样做,请在UICRouteOverlayMapView中设置一个插座并在接口构建器中连接委托,或者在您的viewDidLoad中,对要用来表示的任何实例变量设置委托实际的地图视图。

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.

只需删除以下行即可:

 routeOverlayMapView = [[UICRouteOverlayMapView alloc] init];

如果routeOverlayMapView已经指向您的真实视图。

If routeOverlayMapView is already pointing at your real view.

您可能没有输入最后一个if语句,因为您的委托为零。无论如何,该语句本身是多余的,因为协议中需要该方法。

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.

这篇关于方法代表不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆