2010-07-21 67 views
0

新手問題。我正在構建一個應用程序,我想調用一個我在ViewController中聲明的方法 - 從AppDelegate(在applicationDidBecomeActive上)。如何調用從AppDelegate在ViewController中聲明的方法

所以基本上,在TestAppDelegate.m我有...

 
    - (void)applicationDidBecomeActive:(UIApplication 
*)application { 
    // I want to call a method called "dothisthing" that I've defined in 
FirstViewController.m 
    // This does not work: [FirstViewController dothisthing] } 

在FirstViewController.m我有...

 
- (void) dothisthing { 
    NSLog(@"dothisthing"); 
} 

這是我的第一個iPhone應用程序,所以任何幫助會不勝感激。

回答

3

的方法是一個實例方法,所以你需要先創建實例,然後調用該方法的實例...或聲明的方法爲靜態的(+),而不是( - )虛空

FirstViewController* controller = [FirstViewController alloc]; 

[controller dothisthing]; 

[controller release]; 
+0

感謝您的澄清。我不確定究竟應該在哪裏創建實例。在AppDelegate.m中?完整的Obj-C新手在這裏。 – buckminsterfuller 2010-07-21 16:34:50

+0

那麼,這取決於你的控制器的生命週期...如果這是你的應用程序中的主/唯一控制器,那麼appDelegate應該擁有控制器(在@interface中聲明它),在applicationDidFinishLoading中實例化它並釋放它在dealloc) – Jaime 2010-07-21 16:44:55

+0

謝謝!我有些東西在工作,但一個問題...我收到了警告: FirstViewController可能不是「-dothisthing」 這裏迴應是代碼: FirstViewController *控制器= [FirstViewController頁頭]; \t [controller dothisthing]; //這會拋出警告 – buckminsterfuller 2010-07-21 19:38:00

0

使用這個簡單的NSNotificationCenterexample類似的問題。發揮魅力!

相關問題