使用协议和委托 [英] Using a protocol and a delegate

查看:228
本文介绍了使用协议和委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在视图中获取一些代码。我已经宣布了一个代表,并没有被实例化,任何想法我失踪了?
我已经尝试总结我如何做到这一点。我认为问题在于某处,我的dataSource委托需要被实例化。



我有一个视图调用图形视图和一个在viewcontroller GraphViewController中的委托。 / p>

我知道GraphView中的方法正在做一些事情,因为它调用AxisDrawing助手类并绘制Axes。



以下是相关代码



在GraphView.h中,我设置了协议和dataSourceDelegate

  @class GraphView; 

@protocol GraphViewDataSourceDelegate
- (float)functionOfX:(float)xValue inGraphView:(GraphView *)sender;
@end

@interface GraphView:UIView

@property(nonatomic,weak)IBOutlet id< GraphViewDataSourceDelegate> dataSourceDelegate;

@end

我在GraphView.m中合成

  #importGraphView.h
#importAxesDrawer.h

@implementation GraphView

@synthesize dataSourceDelegate = _dataSourceDelegate;

另外在图形视图中我尝试使用如下的代理(像素是一个CGPoint)。这个例程运行,我可以从这里提供GraphView,因为我不尝试使用我的协议方法。即我的一些DrawRect的东西得到绘制,检查UIView链接到我的自定义视图

  pixel.y = [ self.dataSourceDelegate functionOfX:pixel.x inGraphView:self]; 

++++断点放在这里+++++



在GraphViewController中,我声明我实现协议并实现它如下。编译器会在执行完成后向我发出警告并转发警告。 (我现在只作为测试返回3.0)。

  @interface 

GraphViewController )< GraphViewDataSourceDelegate>
@property(nonatomic,weak)IBOutlet GraphView * graphView;

@end

...

   - (float)functionOfX:(float)xValue inGraphView:(GraphView *)sender {
NSLog(@fofx visited);
返回3.0;
}

如果我在断点后看到GraphView *对象,没有实例。我错过了什么。



这是从断点处的跟踪



_dataSourceDelegate struct objc_object * 0x0



编辑:(除了 @Clays 答案)



原来,我与自定义视图的连接已经破裂。这意味着错误在于View不与Custom ViewController进行通信。该链接是通过ctrl从界面构建器中的视图控制器按钮拖动到ViewController中的视图。



这导致ViewController被实例化,一切都正常工作



换句话说。



有关链接是Outlet。这必须在ViewController中声明为一个属性,然后使用ctrl链接到IB中,从ViewController名称栏拖动到Controller中的视图。



只要你已经正确添加了Outlet属性,当您执行ctrl拖动时,您的视图将显示一个选项。



IB中的ViewController按钮上的上下文按钮弹出信息会给出一个线索。



如果您忽略将出口属性放入视图控制器,该链接确实会出现在上下文按钮弹出窗口中,但它是灰色的,当您执行您的视图未命名的链接。



一旦您将插座放在菜单中,该名称将显示在菜单中,但不会变灰。

解决方案

确保你已经把所有东西都挂钩了:设置GraphView的dataSourceDelegate和GraphViewController的graphView。



从跟踪看起来你没有这样做;或者你在某处失去了参考,因为在某些时候,没有什么是坚持的。


I am trying to get some code in a view working. I have declared a delegate and it does not get instantiated, any Idea what I am missing? I have tried to summarise how I have done this below. I think that the issue is that somewhere, my dataSource delegate needs to be instantiated.

I have a View called graph view and a delegate that is in the viewcontroller GraphViewController.

I know that the method in GraphView is doing something as it calls the AxisDrawing helper class and draws in Axes.

Here is the relevant code

In GraphView.h I set up the protocol and the dataSourceDelegate

@class GraphView;

@protocol GraphViewDataSourceDelegate
- (float)functionOfX:(float)xValue inGraphView:(GraphView *)sender;
@end

@interface GraphView : UIView

@property(nonatomic, weak) IBOutlet id <GraphViewDataSourceDelegate> dataSourceDelegate;

@end

Which I synthesize in GraphView.m

#import "GraphView.h"
#import "AxesDrawer.h"

@implementation GraphView

@synthesize dataSourceDelegate = _dataSourceDelegate;

Also in Graph View I try to use the delegate as follows (pixel is a CGPoint). This routine does run and I can draw to GraphView from here provided I do not try to use my protocol method. i.e. Some of my DrawRect stuff does get drawn which checks out the linking of the UIView to my custom View

pixel.y = [self.dataSourceDelegate functionOfX:pixel.x inGraphView:self];

++++Breakpoint put in here+++++

In GraphViewController I state that I implement the protocol and implement it as follows. The compiler warns me and spots when the implementation is done, turning of the warning. (I am only returning 3.0 at the moment as a test).

@interface  

GraphViewController () <GraphViewDataSourceDelegate>
@property (nonatomic, weak) IBOutlet GraphView *graphView;

@end

...

-(float) functionOfX:(float)xValue inGraphView:(GraphView *)sender{
    NSLog(@"fofx accessed");
    return 3.0;
}

If I look at the GraphView* object just after a breakpoint, it seems to have no instance. What am I missing out.

This is from the trace at the breakpoint

_dataSourceDelegate struct objc_object * 0x0

EDIT: (In addition to @Clays answer below)

It turned out that my connection to the custom view was broke. This meant that the fault lay with the View not talking to the Custom ViewController. The link is made by ctrl dragging from the View Controller button in the Interface builder to the View within the ViewController.

This caused the ViewController to be instantiated and everything then worked fine.

To put it another way.

The link in question was the Outlet. This has to be declared in the ViewController as a property and then linked in IB using ctrl Drag from the ViewController Name-Bar to the View in the Controller.

Provided that you have added the Outlet property correctly, when you do the ctrl drag, your view will appear an option.

The context button popup information on the ViewController button in IB does give a clue.

If you have neglected to put the outlet property into the view controller, the link does appear in the context button popup but it is greyed out, and when you do the link your View is not named.

Once you put the outlet in, the name appears in the menu but not greyed out.

解决方案

Make sure you've hooked everything up correctly: setting your GraphView's dataSourceDelegate, and your GraphViewController's graphView.

From the trace it looks like you haven't done that; or you lose the reference somewhere along the way because at some point nothing's holding on to it.

这篇关于使用协议和委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆