2012-08-14 125 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