如何通过放置在主类中的第二类对象从第一类执行函数?

时间:2011-12-13 20:20:45

标签: objective-c class inheritance reference

我有3节课。 ViewController,WorkspaceView,MainMenuView。 如何从-(void)showMenu -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; WorkspaceView.m执行workspaceView(点击ViewController.m中初始化的对象mainMenuView时)?在workspaceView类中创建了ViewController#import <UIKit/UIKit.h> #import "WorkspaceView.h" #import "MainMenuView.h" @interface ViewController : UIViewController { WorkspaceView *workspaceView; MainMenuView *mainMenuView; } @property (nonatomic, retain) WorkspaceView *workspaceView; @property (nonatomic, retain) MainMenuView *mainMenuView; @end

我希望你理解;)

感谢您的帮助。

ViewController.h

#import "ViewController.h"

@implementation ViewController

@synthesize workspaceView;
@synthesize mainMenuView;

#pragma mark - View lifecycle

- (void)viewDidLoad
{

// WorkspaceView
workspaceView = [[WorkspaceView alloc] initWithFrame:CGRectZero];
[self.view addSubview:workspaceView];
// ---

// MainMenuView
mainMenuView = [[MainMenuView alloc] initWithFrame:CGRectZero];
[self.view addSubview:mainMenuView];
// ---
...
}
...
@end

ViewController.m

#import <UIKit/UIKit.h>
@interface WorkspaceView : UIView
@end

WorkspaceView.h

#import "WorkspaceView.h"
@implementation WorkspaceView
...

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
//howto execute showMenu or hideMenu from MainMenuView using mainMenuView object in ViewController?
NSLog(@"workspace"); // <-it's work;
}

@end

WorkspaceView.m

@interface MainMenuView : UIView
...
-(void)hideMenu;
-(void)showMenu;

@end

MainMenuView.h

#import "MainMenuView.h"

@implementation MainMenuView

- (id)initWithFrame:(CGRect)frame{
...
}


-(void)hideMenu{
self.frame = CGRectMake(0, -1 * self.frame.size.height, self.frame.size.width, self.frame.size.height);
}

-(void)showMenu{
self.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}

@end

MainMenuView.m

{{1}}

1 个答案:

答案 0 :(得分:1)

我将告诉您使用通知中心的方法。您也可以使用委托并设计协议,但这是一种非常简单的方法。

在WorkspaceView.m中:

#import "WorkspaceView.h"
@implementation WorkspaceView
...

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
//howto execute showMenu or hideMenu from MainMenuView using mainMenuView object in ViewController?
[[NSNotificationCenter defaultCenter] postNotificationName:@"SHOW_MENU" object:nil];

NSLog(@"workspace"); // <-it's work;
}

@end
MainMenuView.m中的

#import "MainMenuView.h"

@implementation MainMenuView

- (id)initWithFrame:(CGRect)frame{
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(handleShowMenu:) 
                                             name:@"SHOW_MENU" object:nil];
}

- (void)handleShowMenu:(NSNotification*)note
{
    [self showMenu];
}

-(void)hideMenu{
self.frame = CGRectMake(0, -1 * self.frame.size.height, self.frame.size.width, self.frame.size.height);
}

-(void)showMenu{
self.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}

@end