2

我刚开始使用Objective -C。我的代码不断给出错误:在转发类对象'startPageViewController'中找不到属性'view'。我无法弄清楚它是什么!这是我的代码。

startPageViewController.h -

#import <UIKit/UIKit.h>

@class searchResultsViewController;
@class startPageViewController;

@interface startPageViewController : UIViewController

- (IBAction)switchViews:(id)sender;

@property (strong, nonatomic) IBOutlet UITextField * searchterm;
@property (nonatomic, retain) startPageViewController *blueViewController;
@property (nonatomic, retain) searchResultsViewController *yellowViewController;

@end

startPageViewController.m -

#import "startPageViewController.h"
#import "searchResultsViewController.h"

@class searchResultsViewController;
@class startPageViewController;

@interface startPageViewController ()

@end

@implementation startPageViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (IBAction)switchViews:(id)sender {
    [self.view removeFromSuperview]; // <-- Error here
    [self.view insertSubview:_yellowViewController.view atIndex:0];
}

- (void)viewDidUnload {
    [self setSearchterm:nil];
    [super viewDidUnload];
}
@end

searchResultsViewController.h -

#import <UIKit/UIKit.h>

@interface searchResultsViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIWebView *googleResults;
@property (strong, nonatomic) IBOutlet UIWebView *bingResults;
@property (strong, nonatomic) IBOutlet UIWebView *amazonResults;
@property (strong, nonatomic) IBOutlet UIWebView *ebayResults;

@end

searchResultsViewController.m -

#import "searchResultsViewController.h"

@interface searchResultsViewController ()

@end

@implementation searchResultsViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *googleUrlAddress = @"http://www.google.com/";
    NSURL *googleUrl = [NSURL URLWithString:googleUrlAddress];
    NSURLRequest *googleRequestObj = [NSURLRequest requestWithURL:googleUrl];
    [_googleResults loadRequest:googleRequestObj];
    // --------------------
    NSString *bingUrlAddress = @"http://www.bing.com/";
    NSURL *bingUrl = [NSURL URLWithString:bingUrlAddress];
    NSURLRequest *bingRequestObj = [NSURLRequest requestWithURL:bingUrl];
    [_bingResults loadRequest:bingRequestObj];
    // --------------------
    NSString *amazonUrlAddress = @"http://www.amazon.com/";
    NSURL *amazonUrl = [NSURL URLWithString:amazonUrlAddress];
    NSURLRequest *amazonRequestObj = [NSURLRequest requestWithURL:amazonUrl];
    [_amazonResults loadRequest:amazonRequestObj];
    // --------------------
    NSString *ebayUrlAddress = @"http://www.ebay.com/";
    NSURL *ebayUrl = [NSURL URLWithString:ebayUrlAddress];
    NSURLRequest *ebayRequestObj = [NSURLRequest requestWithURL:ebayUrl];
    [_ebayResults loadRequest:ebayRequestObj];
}

- (void)viewDidUnload {
    [self setGoogleResults:nil];
    [self setBingResults:nil];
    [self setAmazonResults:nil];
    [self setEbayResults:nil];
    [super viewDidUnload];
}
@end

提前致谢!

顺便说一句,请不要太复杂。我只有 12 岁。:)

4

2 回答 2

4

The problem is the extraneous @class declarations in the startPageViewController.m file. Those are hiding the full definition you get from the #import statements.

You should only have @class forward declarations in a .h file (like you are already doing).

One other suggestion - It is standard practice in many programming languages, including Objective-C, that class names begin with an uppercase letter. Variables and method names should begin with lowercase letters. Doing this, along with everyone else, makes code easier to read.

于 2013-03-09T00:36:48.240 回答
0

如果您包含头文件,则不需要转发声明。所以你可以删除:

@class searchResultsViewController;
@class startPageViewController;
于 2013-03-08T21:55:29.987 回答