2010-07-09 31 views
0

我想知道在appDelegate中的類和同一類中導入appDelegate的結果。因爲,我正在成功地在我的應用程序中執行此操作,但建議您不要執行此操作。儘管進行了大量搜索,但我找不到答案。在appDelegate中導入appDelegate類和相同類的後果

在此先感謝。

回答

6

你可以這樣做,但要小心你如何導入標題。這是推薦的方式:

AppDelegate.h

// Import headers here 

@class MyViewController; 

@interface AppDelegate : NSObject <UIApplicationDelegate> { 
    MyViewController *viewController; 
} 

- (void)someMethod; 

@end 

AppDelegate.m

#import "AppDelegate.h" 
#import "MyViewController.h" 

@implementation AppDelegate 

//Your code here 

@end 

MyViewController.h

// Import headers here 

@class AppDelegate; 

@interface MyViewController : UIViewController { 
    AppDelegate *appDelegate; 
} 

@end 

MyViewController.m

#import "MyViewController.h" 
#import "AppDelegate.h" 

@implementation MyViewController 

// Your code here 

@end 

正如你所看到的,你想用@class在你的頭部聲明的類,然後導入頭在你的.m文件。這可以確保你不會導入你不需要的東西;如果您將視圖控制器標頭導入到應用程序委託的標題中,它將被導入到導入應用程序委託標題的任何內容中。通過將所有導入保留爲.m文件,可以防止出現這種情況。

+0

Thanx ...正是我在找什麼..是否與任何類導入到其他類相同的情況,反之亦然? – neha 2010-07-09 18:36:06

+0

是的。在絕對必要時,您只想將其他頭文件包含在頭文件中。這將有助於減小文件大小(不必要的東西不會被編譯到每個文件中)並提高性能,並防止錯誤。 – 2010-07-10 00:06:13