iphone - UIViewController调用对方的delegate

标签 iphone objective-c ipad

我有两个 UIViewController,每个都有它的委托(delegate),并且正在调用其中一个。一个类称为 TopicViewController,另一个类称为 MentionViewController,代码如下所示:

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <RestKit/RestKit.h>
#import "Message.h"
#import "Imgur.h"
#import "URLViewController.h"
#import "CVore.h"
#import "NSData+Base64.h"
#import "Imgur.h"
#import "ProfileViewController.h"
#import "OptionsViewController.h"
#import "Three20/Three20.h"


@class DetailViewController;
@class MentionViewController;

@protocol DetailViewControllerDelegate

- (void) viewController:(DetailViewController*)viewCon withText:(NSString *) text;

@end


@interface DetailViewController : UIViewController <MentionViewControllerDelegate>


///////////////////////////////////////////////////////////////////////////////////

#import <UIKit/UIKit.h>
#import <RestKit/RestKit.h>
#import "Members.h"
#import "DetailViewController.h"
#import "Three20/Three20.h"

@class MentionViewController;

@protocol MentionViewControllerDelegate

- (void) viewController:(MentionViewController*)viewCon withUsername:(NSString *) text;

@end


@interface MentionViewController : UITableViewController <DetailViewControllerDelegate>

现在的问题是,当我将 #import "MentionViewController.h"添加到 DetailViewController 时,它在 MentioViewController 中出现以下错误:

Cannot find protocol declaration for DetailViewControllerDelegate.

我知道这可能是由于循环引用造成的,但我该如何解决这个问题?

最佳答案

确实很奇怪。 MentionViewController需要DetailViewController的头文件,DetailViewController需要MentionViewController的头文件。 这是一个循环。也许您需要创建一个空的头文件,并将所有协议(protocol)放入其中。例如,

MyProtocol.h

@class DetailViewController;
@class MentionViewController;

@protocol DetailViewControllerDelegate

- (void) viewController:(DetailViewController*)viewCon withText:(NSString *) text;

@end

@protocol MentionViewControllerDelegate

- (void) viewController:(MentionViewController*)viewCon withUsername:(NSString *) text;

@end

并在 DetailViewController.h 和 MentionViewController.h 中添加#import MyProtocol.h

关于iphone - UIViewController调用对方的delegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6086514/

相关文章:

iphone - iOS UIImageView vs drawRect 图像拉伸(stretch)

iphone - 为团队创建 iOS 开发证书 - 无效证书

ios - 在ios中刷新tableviewcontroller

ios - Instruments Allocations 跟踪用户定义类的对象的分配和解除分配

iphone - 如何将文件从您的应用程序导出到其他移动应用程序?

ios - Table View 的数据源对象实例保存在哪里比较好?

iphone - 我可以制作具有这些规范的 iPhone 应用程序吗?

ios - 寻找一种更简洁的方式来执行此搜索功能

在 iPad 上运行的 iPhone 应用程序 : rootViewController not the tabBarController as it is when running on iPhone

ios - 仅适用于 iPhone 的方向锁定