グラフ作成アプリを作成しています。ビューとビューコントローラーがあり、ビューのデリゲートとして機能します(情報を取得するため)。実際の描画はまだ開始していませんが、現在、値を辞書に保存しようとしています。ただし、特定のNSLogをView Controller全体に整然と配置していて、ビューから呼び出すデリゲートメソッドがまったく呼び出されないことに気付きました。たとえば、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