2013-03-24 4 views
0

나는 그래프 응용 프로그램을 만들고 있는데, 정보를 검색하기 위해 뷰의 위임자 역할을하는 뷰 컨트롤러와 뷰 컨트롤러가있다. 아직 실제 그림을 시작하지는 않았지만 현재 값을 사전에 저장하려고합니다. 그러나 특정 NSLog가 뷰 컨트롤러에 체계적으로 배치되어 있고 뷰에서 호출하는 대리자 메서드가 전혀 호출되지 않는다는 것을 알았습니다. 예를 들어, 내 scaleForGraph 함수를 호출하지만 실행되지 않습니다. 이것에 대해 새삼 스럽기 때문에 나는 실종 된 것이 있는지 확실하지 않습니다. 참고 : 오류가 없으며 컴파일되고 실행됩니다. 가능한 한 코드를 줄이려고 노력했습니다. 도와 줘서 고마워! 하는 .m 여기대리인이 호출되지 않는다.

// 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 

그리고 것 : 여기

내가 프로토콜을 정의 내보기에 대한 .H의

#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 

답변

4

게시 한 코드에서 GraphViewGraphingViewController이 위임되었음을 알려주지 않습니다. 그래서 nil에게 메시지를 보냅니다. 당신의 GraphingViewController 설정 코드에서

self.graphView.delegate = self; 

:

당신이 그런 짓을 할 수 있습니다.

1

컨트롤러를 GraphView의 실제 대리인으로 만듭니다. 인터페이스 빌더에서 GraphView에서 오브젝트 (오렌지색 원)를 드래그하여 "위임자"를 선택하여 인터페이스 빌더에서 할 수 있습니다.

+0

저는 약간 혼란 스럽습니다. 무엇으로 무엇을 드래그합니까? 편집 : 신경 쓰지 마세요. –

관련 문제