0
モデル(GraphView)とコントローラ(GraphViewController)の間でプロトコル/デリゲートメソッドを動作させるのに問題があります。 のdrawRectでのNSLog: GraphView.mでは表現ががnullプロトコルとビューとコントローラ間のデリゲート
変数表現が値を(のviewDidLoadでのNSLogがこれを証明している)を保持していることを私に伝えます。私は明白な何かを欠いていますか
GraphView.h
@class GraphView;
@protocol GraphViewDelegate
-(NSString *) expressionForGraphView:(GraphView *) requestor;
@end
@interface GraphView : UIView
{
id <GraphViewDelegate> delegate;
}
@property (assign) id <GraphViewDelegate> delegate;
@end
GraphView.m
#import "GraphView.h"
@implementation GraphView
@synthesize delegate;
- (void)drawRect:(CGRect)rect
{
NSString *expression = [self.delegate expressionForGraphView:self];
NSLog(@"%@", expression);
}
GraphViewController.h
#import <UIKit/UIKit.h>
#import "GraphView.h"
@interface GraphViewController : UIViewController <GraphViewDelegate>
{
GraphView *graphView;
NSString *expression;
}
@property (retain) IBOutlet GraphView *graphView;
@property (retain) NSString *expression;
@end
GraphViewController.m
#import "GraphViewController.h"
@implementation GraphViewController
@synthesize graphView;
@synthesize expression;
-(NSString *) expressionForGraphView:(GraphView *) requestor
{
NSString *tempString;
if (requestor == self.graphView)
tempString = self.expression;
else
tempString = nil;
return tempString;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.graphView.delegate = self;
NSLog(@"%@", self.expression);
}
では、あなたはexpressionForGraphView '内の任意のNSLogsを配置している:' ** 'tempString'理由は**' nil'を返していますか? – WrightsCS
あなたのログステートメントはどのような順序で起こっていますか? (おそらくもっと話すためにテキストを追加する必要があります。) –
ViewControllerが最初に呼び出されます。そのため、ViewDidLoadではその式が値を持つことがわかります。問題はありません。しかし、GraphView drawRectが呼び出されると、NSLogはnullになります。 infoの場合、graphViewオブジェクトには、ViewControllerによって呼び出される 'setNeedsDisplay'メソッドがあります。 – John67