Obj-C:没有调用委托方法

8vi*_*ius 3 delegates protocols objective-c ios

我是iOS Dev的新手,我正在关注2010年秋季的斯坦福CS193P课程.我正在完成作业3并且我将我的委托设置为我的视图并使用调试器我注意到对我的委托的调用方法不会发生,我不明白会发生什么.我的代码如下:

GraphViewController.h:

@interface GraphViewController : UIViewController <GraphViewDelegate> {
    GraphView *graphView;
    float scale;
}

@property (retain) IBOutlet GraphView *graphView;
@property float scale;

- (IBAction)zoomIn;
- (IBAction)zoomOut;

@end
Run Code Online (Sandbox Code Playgroud)

GraphViewController.m:

@implementation GraphViewController

@synthesize graphView, scale;

- (NSString *)functionForGraph:(GraphView *)requestor {
    NSLog(@"%@", @"culo peluo");
    return @"lol";
}

- (float)scaleForGraph:(GraphView *)requestor {
    return self.scale;
}

- (IBAction)zoomIn {

}

- (IBAction)zoomOut {

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.graphView.delegate = self;
    self.scale = 20;
    [self.graphView setNeedsDisplay];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
Run Code Online (Sandbox Code Playgroud)

GraphView.h:

@class GraphView;

@protocol GraphViewDelegate 
- (NSString *)functionForGraph:(GraphView *)requestor;
- (float)scaleForGraph:(GraphView *)requestor;
@end

@interface GraphView : UIView {
    id <GraphViewDelegate> delegate;
}

@property (assign) id <GraphViewDelegate> delegate;

@end
Run Code Online (Sandbox Code Playgroud)

GraphView.m:

@implementation GraphView

@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    CGFloat cgScale = [self.delegate scaleForGraph:self];
    [AxesDrawer drawAxesInRect:self.bounds originAtPoint:self.center scale:cgScale]; 
}


- (void)dealloc
{
    [super dealloc];
}

@end
Run Code Online (Sandbox Code Playgroud)

Jer*_*myP 8

  1. 在设置图表视图委托的行上放置一个断点.
  2. 检查graphView变量.它没有?

这种情况一直发生在我身上,并且总是(几乎总是如此),因为我无法将插座连接到界面构建器中的视图.

  • 我的朋友,你太棒了,谢谢你.我是个白痴. (3认同)

wzb*_*zon 5

假设 YourClass 类是其他类的委托。尽管设置了委托属性,但不会调用委托方法。

最有可能的问题是您的类实例(即其他类的委托)在调用委托方法之前被释放。通过使此类成为其他类的属性或实例变量或使用dispatch_once,使其更加持久。例如,

改变

YourClass *instance = [[YourClass alloc] init];
Run Code Online (Sandbox Code Playgroud)

经过

@property(nonatomic, strong) YourClass *instance;
self.instance = [[YourClass alloc] init]; 
Run Code Online (Sandbox Code Playgroud)

出现此问题的原因是,在 ARC 中,当方法完成时,方法内创建的所有内容(而不是实例变量)都会被释放。并且无法调用由某些后台进程调用的委托方法。

我已经就这个问题写了一篇很大的博文。这是很常见的情况。 http://alwawee.com/wordpress/2013/07/31/on-xmppframework-delegate-method-not-being- Called/