Objective c 从RootViewController调用函数

Objective c 从RootViewController调用函数,objective-c,Objective C,如何从FirstViewController调用RootViewController函数? 我将Xcode 4.6与情节提要一起使用 RootViewController.m: -(void)openMenu { ... } FirstViewController: - (IBAction)btnMenu:(id)sender { RootViewController *root = [[RootViewController alloc] init]; [root o

如何从
FirstViewController
调用
RootViewController
函数? 我将Xcode 4.6与情节提要一起使用


RootViewController.m:

-(void)openMenu
{
    ...
}
FirstViewController:

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

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

- (void)openMenu;

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

- (void)openMenu;

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

FirstViewController.h

@protocol FirstViewDelegate;

@interface FirstViewController : UIViewController

@property (strong) id<FirstViewDelegate> delegate;

@end

@protocol FirstViewDelegate <NSObject>

- (void)openMenu;

@end
#import "FirstViewController.h"

@interface RootViewController : UIViewController
<
FirstViewDelegate
>
MainViewController.h

@protocol FirstViewDelegate;

@interface FirstViewController : UIViewController

@property (strong) id<FirstViewDelegate> delegate;

@end

@protocol FirstViewDelegate <NSObject>

- (void)openMenu;

@end
#import "FirstViewController.h"

@interface RootViewController : UIViewController
<
FirstViewDelegate
>

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

FirstViewController.h

@protocol FirstViewDelegate;

@interface FirstViewController : UIViewController

@property (strong) id<FirstViewDelegate> delegate;

@end

@protocol FirstViewDelegate <NSObject>

- (void)openMenu;

@end
#import "FirstViewController.h"

@interface RootViewController : UIViewController
<
FirstViewDelegate
>
MainViewController.h

@protocol FirstViewDelegate;

@interface FirstViewController : UIViewController

@property (strong) id<FirstViewDelegate> delegate;

@end

@protocol FirstViewDelegate <NSObject>

- (void)openMenu;

@end
#import "FirstViewController.h"

@interface RootViewController : UIViewController
<
FirstViewDelegate
>

为什么要从其他控制器调用与UI相关的方法?为什么要从其他控制器调用与UI相关的方法?