简体   繁体   English

声明委托属性时出错

[英]Error when declaring delegate property

This should be an easy question - but I'm having a hard time figuring it out. 这应该是一个简单的问题 - 但我很难搞清楚。 I'm trying to create a property on an object so that during prepareForSegue I can tell the object what it's delegate is. 我正在尝试在对象上创建一个属性,以便在prepareForSegue期间我可以告诉对象它的委托是什么。 I know I could do this with protocols but I figured for this case a direct approach would be simplest. 我知道我可以通过协议来做到这一点,但我认为对于这种情况,直接的方法是最简单的。 Unfortunately, the following code results in a compiler error: 不幸的是,以下代码导致编译器错误:

#import <UIKit/UIKit.h>
#import "PlanningViewController.h"

@interface DepartmentViewController : UITableViewController

@property (nonatomic, weak) PlanningViewController *planningDelegate;

@end

When I type the property declaration, Xcode recognizes PlanningViewController and even displays the text for me to just tab through. 当我输入属性声明时,Xcode会识别PlanningViewController,甚至可以显示我的文本。 The compiler, though, complains with: 但是,编译器抱怨:

Unknown type name 'PlanningViewController': did you mean 'UISplitViewController'?

What am I doing wrong? 我究竟做错了什么?

PlanningViewController.h looks like this: PlanningViewController.h如下所示:

#import <UIKit/UIKit.h>
#import "DepartmentViewController.h"

@interface PlanningViewController : UITableViewController


// Table cell connections
- (IBAction)addItemPressed:(id)sender;


@end

Remove this line from your PlanningViewController.h header file: PlanningViewController.h头文件中删除此行:

#import "DepartmentViewController.h"

You have something of a loop in your header files. 您的头文件中有一些循环。

Better still, make DepartmentViewController.h look like this (there is no need to include PlanningViewController.h in your header file): 更好的是,使DepartmentViewController.h看起来像这样(不需要在头文件中包含PlanningViewController.h ):

#import <UIKit/UIKit.h>

@class PlanningViewController;

@interface DepartmentViewController : UITableViewController

@property (nonatomic, weak) PlanningViewController *planningDelegate;

@end

I think you've kind of missed one of the main points of the delegate patter which is to decouple you objects. 我认为你错过了委托模式的一个主要观点,即将你的物体分开。 The best way of declaring this delegate would be: 声明此委托的最佳方式是:

#import <UIKit/UIKit.h>

@protocol DepartmentViewControllerDelegate; // forward declaration of protocol

@interface DepartmentViewController : UITableViewController

@property (nonatomic, weak) id <DepartmentViewControllerDelegate> delegate;

@end

@protocol DepartmentViewControllerDelegate
- (void)departmentViewController:(DepartmentViewController *)controller
              isProcessingPeople:(NSArray *)people
@end

In your department view controller, you would then write something like this: 在您的部门视图控制器中,您将编写如下内容:

if ([self.delegate respondsToSelector:@selector(departmentViewController:isProcessingPeople:)]) {
    [self.delegate departmentViewController:self isProcessingPeople:people];
}

And in your planning view controller, you would implement this method: 在您的计划视图控制器中,您将实现此方法:

- (void)departmentViewController:(DepartmentViewController *)controller
              isProcessingPeople:(NSArray *)people {
    // do necessary work here
}

The example here is just an example of one message you can send to the delegate. 此处的示例只是您可以发送给代理的一条消息的示例。 You can add whatever you need, but this makes it so there is no coupling between your controllers. 您可以添加所需的任何内容,但这使得控制器之间没有耦合。 The planning view controller knows everything it needs to about the department controller, but the department controller doesn't need to know anything about the planning controller. 计划视图控制器知道部门控制器所需的一切,但部门控制器不需要了解计划控制器的任何信息。

If you want to stick with what you have currently, just recognize that it's not really the delegate pattern, and you should probably rename your property. 如果你想坚持你现在拥有的东西,只要认识到它不是真正的委托模式,你应该重命名你的财产。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM