2011-12-13 79 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

相關問題