从不调用Delegate方法

时间:2012-08-27 21:52:32

标签: ios delegates

第一。这是我的代码。

paintingView.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@protocol PaintingViewDelegate
-(void)makeLine:(CGPoint) touch;
@end
//CLASS INTERFACES:
@interface PaintingView : UIView
{
id <PaintingViewDelegate> delegate;
@private
   CGPoint  location;
   CGPoint  previousLocation;
   Boolean  firstTouch;
}
@property(nonatomic, readwrite) CGPoint location;
@property(nonatomic, readwrite) CGPoint previousLocation;
@property(nonatomic, assign) id <PaintingViewDelegate> delegate;
@end

paintingView.m

#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/EAGLDrawable.h>
#import "PaintingView.h"

//CLASS IMPLEMENTATIONS:
@implementation PaintingView
@synthesize  location;
@synthesize  previousLocation;
@synthesize delegate;
// Implement this to override the default layer class (which is [CALayer class]).
// We do this so that our view will be backed by a layer that is capable of OpenGL ES rendering.
+ (Class) layerClass
{
return [CAEAGLLayer class];
}

// Handles the start of a touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGRect              bounds = [self bounds];
UITouch*    touch = [[event touchesForView:self] anyObject];
firstTouch = YES;
// Convert touch point from UIView referential to OpenGL one (upside-down flip)
location = [touch locationInView:self];
location.y = bounds.size.height - location.y;
    [[self delegate]makeLine:location];
}
...
@end

viewController.h

#import "PaintingView.h"

@interface GMViewController : UIViewController <AGSMapViewLayerDelegate, PaintingViewDelegate>
{
PaintingView *painting1;
}
...
@end

viewController.m

...
- (void)viewDidLoad
{
[super viewDidLoad];

painting1 = [[PaintingView alloc] init];
[painting1 setDelegate:self];
}
...
-(void)makeLine(CGPoint) touch
{
NSLog(@"TOUCH");
}
...
@end

永远不会调用我的委托方法。我已经从iOS-developer.net资源示例中创建了我的委托方法。编译期间没有错误。

1 个答案:

答案 0 :(得分:1)

通过使用该行设置paintingView's委托来修复它  [painting setDelegate: self]; 在我的viewController类文件中。