2014-04-24 49 views
-1

我試圖從另一個類中調用一個(void)方法,該類是ViewController.h文件中的ViewController.h & .m;試圖從另一個類中調用(void)方法

-(void)showMore; 

,並在ViewController.m文件我有

-(void)showMore { 

    //my stuff to show in here 
} 

我從使用此方法BoothViewController調用它;

ViewController *app = (ViewController *)[[UIApplication sharedApplication] init]; 
     [app showMore]; 

它沒有在代碼中顯示任何錯誤,但是它在測試過程中無法識別的選擇器時崩潰。

我也嘗試過沒有成功;

ViewController *app = (ViewController *) [ViewController init]; 
     [app showMore]; 

任何幫助將不勝感激! :)

回答

5

你必須分配和發送消息給它之前初始化你的視圖控制器:

ViewController *myViewController = [[ViewController alloc] init]; 
[myViewController showMore]; 

或者,如果視圖控制器的應用RootViewController的:

ViewController *myViewController = (ViewController*)[UIApplication sharedApplication].delegate.window.rootViewController; 
[myViewController showMore]; 
+0

非常感謝您! :) – user3355723

0

嘗試[[[ViewController alloc] init] showMore];

1

您需要首先alloc控制器:

ViewController *app = (ViewController *) [[ViewController alloc] init]; 

,你不能只是init東西。

0

導入的ViewController類英寸h文件你的看法

#import "ViewController" 
@interface YourClass: UIViewController 
{ 
    ViewController *appView; 
} 

@Property(nonatomic, retain)ViewController *appView; 

@end 

然後.m類做這樣的分配E - 這

-(void)someMethod 
{ 
    self.appView = [[ViewController alloc] init]; 
    [self.appView showMore]; 
} 

這裏是所有從另一個類調用該方法希望它會爲你工作,因爲它爲我做

+0

什麼是伊娃和財產?如果你有財產,伊娃是爲你自動創建的,所以不需要包括伊娃 – Popeye

+0

@Popeye耶謝謝我現在明白了 – alex

相關問題