0

如何从中调用RootViewController函数FirstViewController?我正在使用带有情节提要的 Xcode 4.6。


RootViewController.m:

-(void)openMenu
{
    ...
}

第一视图控制器:

- (IBAction)btnMenu:(id)sender {
    RootViewController *root = [[RootViewController alloc] init];
    [root openMenu]; // No visible @interface for 'RootViewController' declares the selector 'openMenu'
}
4

2 回答 2

3

您必须在标头中声明该方法RootViewController.h。例子

- (void)openMenu;
于 2013-04-18T00:37:38.810 回答
1

这种情况的常见做法是使用委托。您FirstViewController将有一个委托,然后您RootViewController将为实例设置委托,并接收事件的信息。

第一视图控制器.h

@protocol FirstViewDelegate;

@interface FirstViewController : UIViewController

@property (strong) id<FirstViewDelegate> delegate;

@end

@protocol FirstViewDelegate <NSObject>

- (void)openMenu;

@end

第一视图控制器.m

- (IBAction)btnMenu:(id)sender {
    [self.delegate openMenu];
}

主视图控制器.h

#import "FirstViewController.h"

@interface RootViewController : UIViewController
<
FirstViewDelegate
>

主视图控制器.m

-(IBAction)showFirstViewButtonClicked:(id)sender {
    FirstViewController *firstViewController = [[FirstViewController alloc] init];
    firstViewController.delegate = self;
    [self presentViewController:firstViewController animated:YES completion:nil];
}

-(void)openMenu {
   // this will be called when the btnMenu action is fired in the firstViewController
}
于 2013-04-18T00:49:32.383 回答