我正在构建一个图形应用程序,我有一个视图和一个视图控制器,它充当视图的委托(检索信息)。虽然我还没有开始实际绘图,但我目前正在尝试将值存储在字典中;但是,我在视图控制器中有条理地放置了某些NSLog,我注意到我从视图调用的委托方法根本没有被调用。例如,我调用scaleForGraph
函数,但它不执行。对此我不熟悉,我不确定是否有我遗漏的东西。仅供参考:我没有错误,它编译并执行。我试图尽可能减少代码。谢谢你的帮助!
这是我的观点的.h,我定义了协议:
// GraphView.h
#import <UIKit/UIKit.h>
@class GraphView;
@protocol GraphViewDelegate <NSObject>
- (float)scaleForGraph:(GraphView *)requestor;
-(NSMutableDictionary*) valuesForGraph:(GraphView *)requestor withWidth:(float) width;
@end
@interface GraphView : UIView
@property (nonatomic) id <GraphViewDelegate> delegate;
@property (nonatomic) id expressionCopy;
@end
这是.m:
#import "GraphView.h"
@implementation GraphView
@synthesize expressionCopy = _expressionCopy;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.contentMode = UIViewContentModeRedraw;
}
return self;
}
- (void)awakeFromNib
{
self.contentMode = UIViewContentModeRedraw;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
CGContextRef c = UIGraphicsGetCurrentContext();
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat width = screenSize.width;
CGFloat height = screenSize.height;
float scale = [self.delegate scaleForGraph:self];
NSLog([NSString stringWithFormat:@"My Scale: %f",scale]); //always returns 0
NSMutableDictionary *graphValues = [self.delegate valuesForGraph:self withWidth:width];
}
@end
这是我的视图控制器.h:
#import <UIKit/UIKit.h>
#import "GraphView.h"
@interface GraphingViewController : UIViewController <GraphViewDelegate>
@property (weak, nonatomic) IBOutlet GraphView *graphView;
@property (strong, nonatomic) IBOutlet UIStepper *stepper;
- (IBAction) changedScale:(UIStepper *)stepper;
@property (nonatomic) int scale;
@property (nonatomic) id expressionCopy;
@end
这是控制器的.m:
#import "GraphingViewController.h"
@interface GraphingViewController ()
@end
@implementation GraphingViewController
@synthesize expressionCopy = _expressionCopy;
- (void)updateUI
{
self.stepper.value = self.scale;
[self.graphView setNeedsDisplay];
}
- (void)setScale:(int)scale
{
if (scale < 0) scale = 0;
if (scale > 100) scale = 100;
_scale = scale;
[self updateUI];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)changedScale:(UIStepper *)stepper {
self.scale = stepper.value; //this function works fine, but is not delegate/does not get called by view
}
-(float) scaleForGraph:(GraphView *)requestor {
NSLog(@"HI"); //never gets here
}
-(NSMutableDictionary*) valuesForGraph:(GraphView *)requestor withWidth:(float) width {
NSLog(@"Hello2"); //never gets here
}
return xyVals;
}
@end
答案 0 :(得分:4)
您发布的代码中没有任何地方告诉您GraphView
GraphingViewController
是否是代表。因此,您要向nil
发送消息。
你会想做类似的事情:
self.graphView.delegate = self;
在GraphingViewController设置代码中。
答案 1 :(得分:1)
让您的控制器成为GraphView的实际代表。你可以在界面构建器中通过Ctrl-从GraphView拖动到对象(底部的橙色圆圈)而不是选择“委托”来完成它