2011-12-13 2 views
0

私は3つのクラスを持っています。 ViewController、WorkspaceView、MainMenuViewです。 -(void)showMenuの実行方法WorkspaceView.m(オブジェクトworkspaceViewViewController.mで初期化されている場合)に配置された-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;から実行しますか? ViewControllerクラスにmainMenuViewworkspaceViewが作成されます。メインクラスに配置された第2クラスオブジェクトによってファーストクラスから関数を実行する方法は?

ご協力ありがとうございます。

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 

答えて

1

私はあなたに通知センターでそれを行う方法を紹介します。代理人を使用して議定書を作成することもできますが、これは非常に簡単な方法です。 WorkspaceView.mで

:MainMenuView.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 

#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 
+0

興味深いのは、):)それは働いている、助けてくれてありがとう –

関連する問題