1

我使用基於視圖的template.and做了一個簡單的應用程序,並且我只在viewController文件中放入了nslog視圖的didload方法,並且還在applicationDidFinishLaunch方法(在appDelegate中)中檢查了哪個類文件首先被調用。objective C基本問題

運行後,我得到:viewController先運行,然後appdelegate ..但我認爲appdelegate應該先按其他需求調用... plz給我適當的reasion。

注意到--i沒有在我的appDelegate(應用程序didFinishLaunch)中調用viewController(didnot make object)。我使用iOS4的

回答

2

如果您的視圖控制器是AppDelegate中的一個屬性,相似的代碼參考

@interface AppDelegate_Shared : NSObject <UIApplicationDelegate, UIAlertViewDelegate, OMFDataLoadDelegate> { 

    NSManagedObjectModel *managedObjectModel; 
    NSManagedObjectContext *managedObjectContext;  
    NSPersistentStoreCoordinator *persistentStoreCoordinator; 

    UIWindow *window; 

    UITabBarController *tabBarController; 

} 

則可能得到由AppDelegate中被分配時分配。根據蘋果文檔viewDidLoad在視圖加載到內存後運行,這可能有點令人困惑,因爲語言可以讓你相信它是在它被加載到屏幕上的時候。

http://developer.apple.com/iphone/library/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-SW25

移動你的NSLog語句viewDidAppear爲您所期望的結果。這裏有兩個示例代碼片斷,您應該期望加載語句的方式。

ViewController.m

- (void) viewDidLoad { 
    NSLog(@"1st - this occurs when appDelegate allocates this object"); 
} 
- (void) viewDidAppear { 

    NSLog(@"3rd - this should appear after the applicationDidFinishLaunchingStatement"); 
} 

AppDelegate_Shared.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
    NSLog(@"2. Starting AppDelegate_Shared"); 

    [window addSubview:self.tabBarController.view]; 
    [window makeKeyAndVisible]; 

    NSLog(@"4. Leaving AppDelegate_Shared"); 
    return YES; 
} 
+0

感謝特魯瓦什爲你提供了負擔..並且我申請了你所擁有的東西,但順序是1,2,4,3。我的問題是「什麼是代碼流?」我首先在文檔'main.m'中調用'appdelegate class',然後調用其他類來調用我們在applicationDidFinishLaunch方法中編寫的內容..可以解釋這個.. – chandra 2010-08-23 05:15:19

0

如果初始視圖尚未加載那麼顯然該應用程序尚未完成發射。

消息按正確的順序發送。