我建立一個圖形應用程序,我有一個觀點和一個視圖控制器,它充當視圖的委託(檢索信息)。雖然我還沒有開始實際的繪圖,但我正在嘗試將值存儲在字典中;然而,我有一些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
我有點困惑,我該怎麼Ctrl拖動到什麼?編輯:沒關係,明白了! – 2013-03-24 20:35:45