2011-10-12 145 views
1

我試圖實現這個:方法委託不會被調用

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不會被調用?

我知道肯定,該方法

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

叫,因爲這被顯示NSLog(@"mesagge");。請幫助

我做了調試,並在該行設置斷點:

if ([self.delegate respondsToSelector:@selector(drawingSuccessful:)]) 

和我注意到,這不是一個有效的條件......它從未進入支架......所以這 它不是編譯[self.delegate drawingSuccessful:YES];。 那麼,什麼是錯的?

回答

1

是否航線覆蓋地圖視圖已經出現在你的筆尖?在你看來didLoad要創建它的一個新的實例,設置它的委託,然後什麼都沒有。您通常會將其添加到您的子視圖中,除非,正如我所說,它已經存在於您的nib文件中。

如果有,請在UICRouteOverlayMapView中設置插座,並在界面構建器中或在viewDidLoad中連接委託,將委託設置爲用於表示實際地圖視圖的任何實例變量。

這可能只是刪除這條線的問題:

routeOverlayMapView = [[UICRouteOverlayMapView alloc] init]; 

如果routeOverlayMapView已經在你的真實看法指點。

你可能不進入,去年if語句,因爲你的delegate爲零。聲明本身無論如何都是多餘的,因爲該方法在協議中是必需的。

+0

你是真的......我已經指向那個。我刪除了這一行,並用下面的代碼替換它:routeOverlayView.delegate = self;但仍然沒有進入if語句! – adrian

+0

我不知道你需要的if語句(見我的更新),但什麼是代表的價值,現在,當你在'drawRect'? – jrturton

+0

你是對的! – adrian

3

發生這種情況可能是因爲當您分配UICRouteOverlayMapView時,將調用-drawRect:方法。但代表在該行之後。因此,代表從不接收消息。 總體而言,代表應該是零在那個時候。檢查代表是否爲零。

+0

這有什麼解決方案? – adrian

+0

我們從不手動調用-drawRect:方法。相反,我們可以調用UIView的 - (void)setNeedsDisplay實例方法。它會要求系統重新繪製其視圖內容。有關更多信息,請參閱此處的方法說明http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html – 2011-10-12 10:26:44

+0

顯示的代碼中的位置是george手動調用drawRect? – jrturton

1

喬治

更換這種方法,我相信你會得到解決。

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