Tomasz Szulc
Tomasz Szulc

Reputation: 4235

how to execute function from first class by second class object placed in main class?

I've got 3 classes. ViewController, WorkspaceView, MainMenuView. How to execute -(void)showMenu from -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; placed in WorkspaceView.m (when object workspaceView initialized in ViewController.m is clicked)? mainMenuView and workspaceView are created in ViewController class.

I hope you understand ;)

thanks for help.

ViewController.h

#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.m

#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

WorkspaceView.h

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

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?
NSLog(@"workspace"); // <-it's work;
}

@end

MainMenuView.h

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

@end

MainMenuView.m

#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

Upvotes: 0

Views: 90

Answers (1)

zot
zot

Reputation: 131

I'll show you the way to do it with the Notification Center. You could also use delegates and devise a Protocol, but this is a very simple approach.

In 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

in 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

Upvotes: 1

Related Questions