我是 iOS Dev 的新手,我正在学习 2010 年秋季的斯坦福 CS193P 类(class)。我正在做作业 3,我正在将我的委托(delegate)设置为我的 View ,并通过使用调试器我注意到调用我的委托(delegate)方法不会发生,我不明白会发生什么。我的代码如下:
GraphViewController.h:
@interface GraphViewController : UIViewController <GraphViewDelegate> {
GraphView *graphView;
float scale;
}
@property (retain) IBOutlet GraphView *graphView;
@property float scale;
- (IBAction)zoomIn;
- (IBAction)zoomOut;
@end
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
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
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
最佳答案
- 在设置图形 View 委托(delegate)的行上放置一个断点。
- 检查 graphView 变量。是零吗?
这种情况一直发生在我身上,而且总是(几乎总是)因为我未能将 socket 连接到界面生成器中的 View 。
关于objective-c - Objective-C : Delegate method not getting called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8805580/