在接口/协议中声明的委托?

时间:2011-07-06 14:22:16

标签: objective-c delegates protocols

你能帮我理解代表和协议吗:
在这段代码中:

          @protocol FirstViewControllerDelegate;  
            @interface FirstViewController : UIVIewController {
            …
            id<FirstViewControllerDelegate> delegate;
            }
            @property (assign) id <FirstViewControllerDelegate> delegate;
  • 第一行是什么意思“@protocol”?我没有在我的项目中看到这个委托文件,并且当前文件(带有@interface)似乎是我的FirstViewController的“简单”界面,所以我有点困惑。
  • 我已经看过如何使用委托模式,但我从未见过我们需要在.h中“声明”委托,是否必须在.h文件中写入(我是谈论id&lt; ....&gt; delegate)?

感谢您的回答

1 个答案:

答案 0 :(得分:1)

@protocol FirstViewControllerDelegate;
该格式的

是前瞻性声明。它告诉编译器FirstViewControllerDelegate是一个有效的协议,将在稍后定义(有时在同一个.h文件中进一步向下)。它是必需的,因为没有它,编译器会在看到行

时抱怨
id<FirstViewControllerDelegate>

因为它没有看到它的声明。

实际协议可以定义为

@protocol FirstViewControllerDelegate
{
@required
  - (void)myImportantDelegateMethod;
}