2011-12-13 78 views
0

我有3个班。 ViewController,WorkspaceView,MainMenuView。 如何执行-(void)showMenu-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;置于WorkspaceView.m(当点击ViewController.m初始化对象workspaceView时)? mainMenuViewworkspaceView创建于ViewController类。如何通过放置在主类中的第二个类对象从第一个类执行函数?

我希望你能理解;)

感谢您的帮助。

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:

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

有趣,非常感谢您的帮助,它的工作:)) – 2011-12-13 20:46:51

相关问题