我是iOS开发新手,遇到头文件问题。我遇到了我的头文件的循环依赖问题。我的应用程序委托类包含一个指向我的视图控制器,因为我必须设置视图控制器在我的didFinishLaunchingWithOptions方法的属性之一...如何解决这个循环依赖?
//appDelegate.h //DISCLAIMER: THIS IS UNTESTED CODE AND WRITTEN ON THE FLY TO ILLUSTRATE MY POINT
#import <UIKit/UIKit.h>
#import "MyViewController.h"
@interface appDelegate
NSManagedObjectContext *managedObjectContext;
MyViewController *viewController;
BOOL myFlag;
@end
//appDelegate.m
@implementation appDelegate
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
viewController.managedObjectContext = self.managedObjectContext;
.
.
.
}
@end
而且在我的视图控制器中,我引用了“myFlag”属性,这是在我的应用程序委托...
//MyViewController.h
#import "appDelegate.h" //<---circular dependency, causing "Expected specifier-qualifier-list before MyViewController" errors in my appDelegate header file
@interface MyViewController: UIViewController
{
NSManagedObjectContext *managedObjectContext;
}
@end
//MyViewController.m
@import "MyViewController.h"
@implementation MyViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
((appDelegate*)[[UIApplication sharedApplication] delegate]).myFlag = NO;
}
@end
但为了访问我的应用程序委托“myFlag”属性,我需要导入应用程序委托的头文件。这是导致循环依赖的原因。不知道如何解决这个问题,有没有人遇到过这个问题?
在此先感谢您的帮助!
+1 ..伟大的问题。 – iwasrobbed 2010-08-15 01:06:19
我喜欢它。 \t 帮助 5更多去.. – Mel 2011-05-18 23:52:54