我正在构建一个图形应用程序,我有一个视图和一个视图控制器,它充当视图的委托(检索信息)。虽然我还没有开始实际绘图,但我目前正在尝试将值存储在字典中;但是,我有条不紊地将某些 NSLogs 放置在视图控制器中,并且我注意到我从视图调用的委托方法根本没有被调用。例如,我调用了我的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