【问题标题】:View Delegate Not Being Called查看未调用代理
【发布时间】:2013-03-14 06:22:36
【问题描述】:

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

【问题讨论】:

    标签: ios xcode model-view-controller delegates protocols


    【解决方案1】:

    使您的控制器成为 GraphView 的实际代表。您可以在界面构建器中通过 Ctrl 键从 GraphView 拖动到对象(底部的橙色圆圈)然后选择“委托”

    【讨论】:

    • 我有点迷茫,我ctrl拖到什么地方?编辑:没关系,明白了!
    【解决方案2】:

    在您发布的代码中,您没有告诉您的GraphView GraphingViewController 是它的代表。因此,您正在向nil 发送消息。

    你会想做这样的事情:

    self.graphView.delegate = self;
    

    在您的 GraphingViewController 设置代码中。

    【讨论】:

      猜你喜欢
      • 2015-10-27
      • 2012-10-18
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 2018-05-11
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多