试图从另一个类调用(void)方法

时间:2014-04-24 12:49:50

标签: ios objective-c

我试图从另一个类调用(void)方法,该类是ViewController.h&我在ViewController.h文件中的.m;

-(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];

任何帮助将不胜感激! :)

4 个答案:

答案 0 :(得分:5)

在向其发送消息之前,您必须先分配并初始化视图控制器:

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

或者,如果View控制器是应用程序rootViewController:

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

答案 1 :(得分:1)

您需要先alloc您的控制器:

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

你不能只是init

答案 2 :(得分:0)

尝试[[[ViewController alloc] init] showMore];

答案 3 :(得分:0)

导入视图的.h文件中的ViewController类

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

@Property(nonatomic, retain)ViewController *appView;

@end 

然后.m Class就这样分配它

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

以下是All从另一个类调用该方法希望它能为你工作

相关问题