2014-04-24 124 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

相关问题