objective-c - Objective-C : Delegate method not getting called

标签 objective-c ios delegates protocols

我是 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

最佳答案

  1. 在设置图形 View 委托(delegate)的行上放置一个断点。
  2. 检查 graphView 变量。是零吗?

这种情况一直发生在我身上,而且总是(几乎总是)因为我未能将 socket 连接到界面生成器中的 View 。

关于objective-c - Objective-C : Delegate method not getting called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8805580/

相关文章:

objective-c - 使用 arc4random 随机化 float ?

ios - 如何比较同一个 Swift 枚举的不同实例?

ios - XCode 提示自动补全很慢

javascript - TabNavigator 将不同的参数从每个屏幕传递到 StackNavigator header

c# - 如何接受任何委托(delegate)作为参数

ios - Twitter登录按钮出现问题

iphone - 左侧的 UITableView 图像

javascript - Parse - Obj-C 到云代码

ios - 为什么我会收到此委托(delegate)代码的错误?

c# - 检查方法与给定委托(delegate)的兼容性?