【问题标题】:delegate method not getting called委托方法没有被调用
【发布时间】:2013-08-01 01:03:21
【问题描述】:

我已经查看了具有相同问题的所有其他问题,但我似乎无法听到有关它的信息。我很确定我做的一切都是正确的,因为这不是我第一次使用代表。

//PDFView.h
@class PDFView;

@protocol PDFViewDelegate <NSObject>
-(void)trialwithPOints:(PDFView*)pdfview;
@end

@interface PDFView : UIView
@property (nonatomic, weak) id <PDFViewDelegate> delegate;

在实现文件中,我试图从视图的 touchesMoved 委托调用委托方法

//PDFView.m
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.delegate trialwithPOints:self];
}

实现委托方法的类

//points.h
#import "PDFView.h"
@interface points : NSObject <PDFViewDelegate>

//points.m

//this is where the delegate is set
- (id)init
{
if ((self = [super init]))
{   
      pdfView = [[PDFView alloc]init];
      pdfView.delegate = self;

}
 return self;
 }

-(void)trialwithPOints:(PDFView *)pdf
{
    NSLog(@"THE DELEGATE METHOD CALLED TO PASS THE POINTS TO THE CLIENT");
}

所以这就是我编写委托的方式,不知何故委托为 nil,委托方法从未被调用。

目前我没有对代理做任何事情,我只是想看看它是否正常工作。

对此的任何建议将不胜感激。

【问题讨论】:

  • 你在哪里设置代理?
  • 对不起,我编辑了我的代码。我忘了复制我设置委托的代码。

标签: objective-c ios6 delegates xcode4.5


【解决方案1】:

我认为这是因为你没有持有对委托实例的引用,它被释放是因为它被声明为weak。你可能会这样做:

pdfView.delegate = [[points alloc] init];

您应该将其修复为:

_points = [[points alloc] init];
pdfView.delegate = _points;

其中_points 是实例变量。

【讨论】:

  • 这非常有帮助:)。我确实忽略了这一点。
猜你喜欢
  • 2013-12-03
  • 2020-11-28
  • 2017-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多