2012-08-14 108 views
0

这可能是一个简单的错误,但我似乎无法找出错误Unknown type name 'TransportViewController'的错误。我试图通过xCoor和yCoor这是2 double值到我的第二个视图是TransportViewController。这里是我的代码:未知类型名称'TransportViewController'

TransportViewController *xCoor; 
TransportViewController *yCoor; 
@property (retain, nonatomic) TransportViewController *xCoor; 
@property (retain, nonatomic) TransportViewController *yCoor; 

这4行是给我的错误

MapViewController.h文件

#import "TransportViewController.h" 
@interface MapViewController : UIViewController{ 
    TransportViewController *xCoor; 
    TransportViewController *yCoor; 
} 
@property (retain, nonatomic) TransportViewController *xCoor; 
@property (retain, nonatomic) TransportViewController *yCoor; 

MapViewController.m文件

#import "TransportViewController.h" 
@implementation MapViewController 
@synthesize xCoor; 
@synthesize yCoor; 
. 
. 
. 
- (IBAction) publicTransportAction:(id)sender{ 
    TransportViewController *view = [[TransportViewController alloc] initWithNibName:nil bundle:nil]; 
    self.xCoor = view; 
    self.yCoor = view; 
    xCoor.xGPSCoordinate = self.mapView.gps.currentPoint.x; 
    yCoor.xGPSCoordinate = self.mapView.gps.currentPoint.y; 
    [self presentModalViewController:view animated:NO]; 
} 

TransportViewController.h文件

#import "MapViewController.h" 
@interface TransportViewController : UIViewController<UITextFieldDelegate> 
{ 
    double xGPSCoordinate; 
    double yGPSCoordinate; 
} 
@property(nonatomic)double xGPSCoordinate; 
@property(nonatomic)double yGPSCoordinate; 
@end 

回答

1

你有一个循环依赖。总之,你已指示编译:

  • MapViewController.h需要TransportViewController.h
  • TransportViewController.h需要MapViewController.h

实际上 - 既不是在头必要的。在这两种情况下,您都可以使用转发声明

MapViewController.h

@class TransportViewController; // << forward declaration instead of inclusion 

@interface MapViewController : UIViewController { 
    TransportViewController *xCoor; 
    TransportViewController *yCoor; 
} 
@property (retain, nonatomic) TransportViewController *xCoor; 
@property (retain, nonatomic) TransportViewController *yCoor; 
@end 

TransportViewController.h

@class MapViewController; // << not even needed, as MapViewController 
          // does not exist in this header 

@interface TransportViewController : UIViewController<UITextFieldDelegate> 
{ 
    double xGPSCoordinate; 
    double yGPSCoordinate; 
} 
@property(nonatomic)double xGPSCoordinate; 
@property(nonatomic)double yGPSCoordinate; 
@end 

那么你#import S可在*.m文件去需要的地方。

你应该阅读前瞻性声明。你不能在任何地方使用它们,但是你可以在头文件中使用它们而不是#import,这样可以真正减少构建时间。

+0

感谢您的帮助和建议:)但对于TransportViewController.h'@class MapViewController;'是需要的,因为我只在这里粘贴了部分代码。 – sihao 2012-08-14 05:01:20

+0

@ user1495988是有道理的 – justin 2012-08-14 05:27:05