0

这可能是一个简单的错误,但我似乎无法找出错误的问题所在Unknown type name 'TransportViewController'。我正在尝试将 2double值的 xCoor 和 yCoor 传递给我的第二个视图,即 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
4

1 回答 1

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在需要的地方进入文件。

你应该阅读前向声明。你不能在任何地方使用它们,但是你可以经常在 headers 中使用它们而不是#import,并且可以真正减少你的构建时间。

于 2012-08-14T03:52:58.030 回答