1

#import .h ファイルの代わりに .m ファイルを使用する方法はありますか? 問題は、View Controller が.m ファイルにインポートされているADBannerViewDelegateかどうかを認識しない .h ファイルにあることを指定する必要があることです。iAdこれを回避する方法はありますか、またはそのビューコントローラーを#import iAd毎回使用する必要がありますか?#import

4

1 に答える 1

0

はい。すべての iAd コードを.mファイルに入れることができます。クラス拡張を使用するだけです (非常に一般的です)。クラス拡張により、変数の宣言、デリゲートのインクルード、プロパティの作成などをすべて.mファイルから行うことができます。

クラス拡張は、ステートメント.mの前のファイルの先頭近くにあります。@implementation

例えば:

//.h
#import <UIKit/UIKit.h>
@interface HomeViewController : UIViewController
@end


//.m
#import "HomeViewController.h"
#import <iAd/iAd.h>

//The following is the class extension
@interface HomeViewController () <ADBannerViewDelegate> //add any delegates here {
    IBOutlet ADBannerView *ad;  //A reference to the ad
    BOOL someBOOL;              //You can put any variables here
}
- (void)someMethod:(id)sender;
@property (nonatomic, strong) UIView *someView;
@end

注: クラス拡張子は で終わる必要があり@end、その後に通常のクラス本体が続きます。@implementation HomeViewController...

Apple のドキュメントは、クラス拡張についてさらに詳しく説明しています。ここでそれらをチェックしてください


また、注目に値するのは、「プリコンパイル済みヘッダー」と呼ばれるプロジェクトで自動的に作成される特別なファイルです。このファイルは、プロジェクト全体で使用する予定の他のクラスをインポートできる場所であるため、すべてのクラスでそれらを手動でインポートする必要はありません。

PCH の例を次に示します。

#import <Availability.h>

#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import <iAd/iAd.h>
    //Put any other classes here and you can use them from any file
#endif

プロジェクト ファイルを確認すると、[サポート ファイル] の下に次のように表示されます。You-Project-Name-Prefix.pch

于 2013-07-15T04:32:51.703 に答える